[英]Can I access another project's Embedded Resource from a different project in the same solution?
I have one xml file that stands as an embedded resource in Project_A.我有一个 xml 文件,它作为 Project_A 中的嵌入式资源。
And I want to reach this embedded resource from Project_B which references Project_A.我想从引用 Project_A 的 Project_B 访问这个嵌入式资源。
(Basically Project_B consists of unit tests and I run them with ReSharper.) (基本上 Project_B 由单元测试组成,我使用 ReSharper 运行它们。)
Is it possible to access embedded resource of Project_A when I am working on Project_B?当我在 Project_B 上工作时,是否可以访问 Project_A 的嵌入式资源?
Or in general, can I access another project's Embedded Resource from a different project in the same solution?或者一般来说,我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗?
Yes, you can.是的你可以。 Here is an example, how you can get the embedded resource
System.Timers.Timer.bmp
inside of the .NET Framework's System.dll
:这是一个示例,您如何在 .NET 框架的
System.dll
中获取嵌入式资源System.Timers.Timer.bmp
:
using (Stream stream = typeof(System.Timers.Timer).Assembly.
GetManifestResourceStream("System.Timers.Timer.bmp"))
{
using (Bitmap bitmap = new Bitmap(stream))
{
//bitmap.Dump();
}
}
... or to get the list of the resource names: ...或获取资源名称列表:
string[] names = typeof(System.Timers.Timer).Assembly.GetManifestResourceNames();
... just replace typeof(System.Timers.Timer)
with a type inside of the assembly containing your embedded resources . ...只需将
typeof(System.Timers.Timer)
替换为包含嵌入式资源的程序集内的类型。
Have you tried opening your embedded resource in the "table" view (double-click on Resources.resx file in Visual Studio) and changing the "Access Modifier" in the top strip (the one where "Strings", "Add Resource", "Remove Resource" etc. are placed) to Public instead of internal?您是否尝试过在“表”视图中打开嵌入式资源(双击 Visual Studio 中的 Resources.resx 文件)并更改顶部条中的“访问修饰符”(“字符串”、“添加资源”、 “删除资源”等被放置)公共而不是内部?
You can use the resource editor to mark the whole set of resources as public, but it's an all-or-nothing solution;您可以使用资源编辑器将整套资源标记为公开,但这是一个全有或全无的解决方案; without hand-editing code that is regenerated whenever you edit the resources, you cannot mark individual strings as public.
如果没有在编辑资源时重新生成的手动编辑代码,则无法将单个字符串标记为公开。 Once you have them so marked, you need only a reference to the other assembly to be able to read them.
一旦你对它们进行了如此标记,你只需要对另一个程序集的引用就可以读取它们。 I discovered this technique several years ago, and used it to make commonly used resource strings visible to other assemblies.
几年前我发现了这种技术,并用它使常用的资源字符串对其他程序集可见。
The class that is automatically created by Visual Studio for your embedded resource is internal. Visual Studio 为您的嵌入式资源自动创建的 class 是内部的。
This class cannot be access from a different assembly.此 class 无法从其他组件访问。
You can:你可以:
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.