如何将资源嵌入到另一个项目中

如何将资源嵌入到另一个项目中

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

问题描述

让我们说我有一个C#类库项目,该项目仅包含xml文件作为嵌入式资源。我想从另一个解决方案项目访问这些资源。由于'class'库不包含任何类,因此很难像这样获得程序集:

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 ...

最终获得嵌入式资源。有没有一种方法可以直接获取嵌入式资源而无需对任何魔术字符串等进行硬编码?谢谢。

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.

推荐答案

您可以使用以便从嵌入式程序集加载xml文件。

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

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");

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

08-19 01:44