开发者

Can I access another project's Embedded Resource from a different project in the same solution?

开发者 https://www.devze.com 2023-03-21 04:48 出处:网络
I have one xml file that stands as an embedded resource in Project_A. And I want to reach this embedded resource from Project_B which references Project_A.

I have one xml file that stands as an embedded resource in Project_A.

And I want to reach this embedded resource from Project_B which references Project_A.

(Basically Project_B consists of unit tests and I run them with ReSharper.)

Is it possible to access embedded resource of Project_A when I am working on Project_B?

Or in general, can I access another project's Embedded Resource from a diff开发者_开发技巧erent 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:

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.


The class that is automatically created by Visual Studio for your embedded resource is internal.

This class cannot be access from a different assembly.

You can:

  1. Change this class to public (not sure this will stand if you make further changes to the resources through 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.


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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号