简体   繁体   English

我可以从同一解决方案中的不同项目访问另一个项目的嵌入式资源吗?

[英]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:你可以:

  1. Change this class to public (not sure this will stand if you make further changes to the resources through Visual studio.将此 class 更改为 public(如果您通过 Visual Studio 对资源进行进一步更改,则不确定这是否会成立。
  2. Expose this class by returning it from some main "global" class in your Project_A, and so providing access to it to other "consuming" code.通过从 Project_A 中的某个主要“全局”class 返回此 class 来公开此 class,从而提供对其他“消费”代码的访问。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 我可以从同一解决方案中的另一个项目访问项目的类和方法吗? - Can I access a project's class and methods from an another project in the same solution? 从相同解决方案C#的另一个项目访问资源文件 - Accessing Resource files from another project of same solution C# 同一解决方案中如何从其他项目访问Web API项目访问列表? - How can Web API project access list from another project in same solution? 在同一个解决方案中,如何从另一个项目获得相对于项目的相对路径? - How can I get the relative path to a project from another project in the same solution? 如何从同一解决方案中的单独项目引用资源 (png) - How can I reference a resource (png) from a seperate project in the same solution 在同一解决方案中访问来自其他项目的表单的控件 - To access controls of a form from a different project in same solution 从同一解决方案中的另一个项目访问 appsettings.json 很热吗? - Hot to access appsettings.json from another project in same solution? 在相同解决方案下从另一个项目访问文件 - access file from another project under same solution 如何在同一解决方案中访问另一个项目的服务 - How to access service of another project in same solution 重定向到同一解决方案的不同项目中的另一个页面 - Redirect to another page in different project of same solution
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM