获得嵌入的资源在另一个项目 [英] get embedded resource in another project

查看:108
本文介绍了获得嵌入的资源在另一个项目的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

让我们说我有一个C#类库项目,其中只包含XML文件作为嵌入的资源。我想从另外一个解决方案项目访问这些资源。作为类库中没有的类是相当难以得到大会是这样的:

Let us say I have a C# class library project, which only contains xml files as embedded resources. I would like to access these resources from another solution project. As the 'class' library contains no classes it is quite hard to obtain the assembly like this:

typeof(ClassName).Assembly ...

最终到达嵌入的资源。有没有办法去嵌入的资源,而无需硬code任何魔法串等?谢谢你。

to eventually get to the embedded resources. Is there a way to get to the embedded resources without having to hard code any magic strings etc.? Thanks.

PS:

这似乎是唯一的出路可能在此刻:

This seems the only way possible at the moment:

var assembly = typeof(FakeClass).Assembly;
var stream = assembly.GetManifestResourceStream("Data.Blas.xml"); 

我在数据组件创建了一个假类的。

I have created a 'fake class' in my 'data' assembly.

推荐答案

您可以使用<一个href="http://msdn.microsoft.com/en-us/library/system.reflection.assembly.getmanifestresourcestream.aspx">Assembly.GetManifestResourceStream()从嵌入式assemnly加载XML文件。

you can use Assembly.GetManifestResourceStream() to load the xml file from the embedded assemnly.

System.IO.Stream s = this.GetType().Assembly.GetManifestResourceStream("ActivityListItemData.xml"); 

修改

您可以使用的Assembly.Load()和加载目标程序集,并从那里读取资源。

You can use Assembly.Load() and load the target assembly and read the resource from there.

Assembly.LoadFrom("Embedded Assembly Path").GetManifestResourceStream("ActivityListItemData.xml");

这篇关于获得嵌入的资源在另一个项目的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆