Click here to Skip to main content
15,867,141 members
Articles / Programming Languages / C#
Article

Using Resource-only Assemblies with C#

Rate me:
Please Sign up or sign in to vote.
4.04/5 (20 votes)
17 Apr 20022 min read 167.4K   2.9K   39   14
This note describes how to use resource-only assemblies with C# in the context of a multi-assembly project.

Introduction

This article describes a technique that can be used in order to use resource-only assemblies within a multi-assembly project. I'm assuming that the reader is familiar with basics of assemblies and resource management under .NET. A good introduction in this topic can be found in "Resources and Localization Using the .NET Framework SDK", .NET framework tutorial.

The tutorial describes the technique of using resource files that are embedded within the application (or assembly):

C#
using System.Resources;
[...].ResourceManager rm = new 
  ResourceManager("MyNamespace.MyStrings", this.GetType().Assembly); 
string someString = rm.GetString("SomeString");

In the above example we're creating an instance of ResourceManager class. This instance will allow access to the "Strings" resource file. The second parameter specifies the assembly that embeds the resource. In this case we're specifying the assembly that contains this class. As the second line illustrates, once the resource manager is created, it can be used to get strings or objects from the resource.

So what if the resource you're trying to use is defined in some other assembly? This case is frequent in lager projects, where the code base is separated in multiple modules. Many projects may benefit from locating its resources in separate resource-only assemblies. The sample code in this article includes two projects: a simple console application and a resource-only library. The code demonstrates how the console application accesses strings stored in the resource-only library.

In order to create ResourceManager that corresponds to the resource in the different assembly, we need to specify the reference to the resource assembly as the second parameter in the constructor. Unfortunately Assembly class cannot return the assembly reference based on the resource name. To work around this problem, we first need to create a dummy ResLibraryClass class within resource assembly ResLibrary. Now we can use the dummy class in order to obtain the reference to the resource assembly by using typeof(ResLibraryClass) operator:

C#
using System.Resources;
[...].ResourceManager rm = new ResourceManager("ResouceSamlpe.Strings",
                     Assembly.GetAssembly(typeof(ResLibraryClass))); 
string someString = rm.GetString("string1");

Here we're using static GetAssembly method in order to obtain the reference to the assembly that embeds the resource. Also, note that you must supply the name of the resource file with its namespace, even if both the calling code and the resource file share the same namespace.

License

This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


Written By
Web Developer
Canada Canada

Comments and Discussions

 
QuestionCrystal Reports & resource manager Pin
ib0004221-Jun-07 0:17
ib0004221-Jun-07 0:17 
GeneralVery helpful article. And a hint for vs 2005 users Pin
Sean Yixiang Lu16-May-07 17:17
Sean Yixiang Lu16-May-07 17:17 
GeneralGood one dude Pin
ITstud29-Mar-07 20:27
ITstud29-Mar-07 20:27 
QuestionHow to avoid hard coded strings Pin
Chakravarthy Sayani5-Mar-07 4:51
Chakravarthy Sayani5-Mar-07 4:51 
AnswerRe: How to avoid hard coded strings Pin
Leblanc Meneses7-Mar-07 20:40
Leblanc Meneses7-Mar-07 20:40 
AnswerRe: How to avoid hard coded strings Pin
cisjokey11-Dec-08 22:54
cisjokey11-Dec-08 22:54 
QuestionAvoiding hardcoding Pin
Chakravarthy Sayani5-Mar-07 4:50
Chakravarthy Sayani5-Mar-07 4:50 
QuestionIs this not a better way? Pin
NPowDev6-Feb-06 10:22
NPowDev6-Feb-06 10:22 
You made a nice example, but I thing there is a better way to get the assembly! Smile | :)
I don't like very much the usage of a dummy class to get the Assembly.

So here is my pure speculation that later has turned to be a solution (by copy & paste from the .NET reference help Big Grin | :-D ):
[...]<br />
Assembly ResAssembly;<br />
ResAssembly = Assembly.LoadFrom("c:\\Res.Assembly.dll");<br />
[...].br />

Well, just a suggestion from me. Smile | :)

NPow

PS: Sorry but my english become a bit rusty! Sigh | :sigh:
General.resX and .resource files Pin
gordingin29-Jan-04 5:52
gordingin29-Jan-04 5:52 
GeneralRe: .resX and .resource files Pin
«_Superman_»8-Feb-04 22:55
professional«_Superman_»8-Feb-04 22:55 
GeneralSource code updated Pin
alexis18-Apr-02 4:01
alexis18-Apr-02 4:01 
GeneralPlease FIX this Pin
Ben Kloosterman17-Apr-02 16:32
Ben Kloosterman17-Apr-02 16:32 
GeneralCannot open demo project Pin
Deyan Petrov16-Apr-02 21:33
Deyan Petrov16-Apr-02 21:33 
GeneralRe: Cannot open demo project Pin
diptidjadhav30-Jun-07 1:08
diptidjadhav30-Jun-07 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.