pencilCake
pencilCake

Reputation: 53243

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.

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 different project in the same solution?

Upvotes: 5

Views: 9057

Answers (4)

David A. Gray
David A. Gray

Reputation: 1075

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.

Upvotes: 0

ulrichb
ulrichb

Reputation: 20054

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.

Upvotes: 9

Grzesiek Galezowski
Grzesiek Galezowski

Reputation: 765

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?

Upvotes: 0

lysergic-acid
lysergic-acid

Reputation: 20050

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.

Upvotes: 0

Related Questions