This project is read-only.

Strongly Typed Resource Classes

New to .NET 2.0 and Visual Studio 2005 was the automatic generation of strongly typed resource classes. This is a feature akin to Typed DataSets in that a class is generated to provide a strongly typed interface to the resources from a set. So instead of syntax like the following:

System.Resources.ResourceManager rm = new System.Resources.ResourceManager("MyApplication.MyResource", Assembly.GetExecutingAssembly());
string myString = rm.GetString("myStringKey");
int myInt = (int)rm.GetObject("myIntKey");

Suppose VS2005 had generated a class with the single type Resources then instead you can do something like the following:

string myString = Resources.myStringKey;
int myInt = Resources.myIntKey;

That is a lot neater and friendlier don't you think?

The problem with this is that it is only available for assembly embedded resources sourced from resources extracted from an Xml resource file so this feature has been added to the Resource Application Block to generate strongly typed resource classes for any resource provider type.

To generate such a class you need to configure your resource managers first, once you have done this then you can click on Generate Resource Class from the Resource Provider node context menu:

Generating a Strongly Typed Resource Class

If you had not saved your configuration changes then you are prompted to do so now. Once saved you are asked to select a folder and file name to write the resource class to:

Selecting a Folder and File for the Resource Class

Note: the file filter allows you to filter for either C# files or VB files. The file extension determines the programming language used for the generated code so even if you are creating the class for the first time it is imperative that you make sure the correct filter is chosen so that the correct extension is applied to your file and thus the correct programming language generated. The next dialog asks for a namespace. This will be the namespace applied to your generated class. If you are updating an existing program then the file will be read and the namespace extracted so that you do not have to type it in each time. If you leave it blank then Resources will be used as the namespace.

Setting the Resource Class Namespace

The file is generated containing your strongly typed resource class and a message is displayed giving you the result, path name and base name for the generated class. Note: the class name is taken from the base name of the resource provider:

Strongly Typed Resource Class Generated

When you compile the class it will be added to your assembly. You might have several configured resource managers in which case you might want a resource class for each one. The base names must be unique to avoid having clashing types.

Using the Strongly Typed Resource Class

The class and all its properties is static so there is no need to create an instance of the type. Now you can use resources in a similar fashion to the second example above but just for clarity I will repeat it here; this time we generated a class from a resource manager with a base name of MyResources and used a Namespace name of EntLibContrib.Resources selecting C# as the programming language:

using EntLibContrib.Resources;
string myString = MyResources.myStringKey;
int myInt = MyResources.myIntKey;

Of course, if you want the string formatting feature then you will need to resort to the standard String.Format() way of doing it.

Updating a Strongly Typed Resource Class

This is a simple process of finding the original file and regenerating it followed by recompiling your application. The file is read to extract the namespace so no typing is required if you wish to keep all the existing details the same.

Last edited Feb 28, 2008 at 9:02 PM by ewdev, version 10


No comments yet.