Storing and Retrieving Assembly Resources


If you have experience with storing and retrieving resources in assemblies from previous versions of the .NET Framework, you'll be pleasantly surprised at how easy it is to do in C# 2.0.

Many applications need resources. These resources can be things like images, icons, files, or strings that can be displayed in multiple languages. Developers often embed these resources in their assemblies so that they are easy to find, easy to use, and won't accidentally be deleted from the disk. The resources are often so critical to the deployment of the application that, by embedding them within an assembly, they become a fixed part of the deployment and can't be removed.

The process of adding resources to a project is quite simple in Visual Studio 2005. The new resource manager is extremely easy to use and allows you to organize all of your resources quickly and easily.

The first step is to add a resource file to your project. To add a resource to the project, right-click your project and choose Add and then select New Item. Find the Resources File item template and choose a name for the resources. By having multiple resource files in your project, you can organize resources categorically and make things easier to find.

A feature new to Visual Studio 2005 is the addition of a wrapper class that provides strongly typed members that expose the resources you create in your resource file. Each resource file in your project (it has a .resx extension) is converted into a class at compile time by Visual Studio.

In this way, if you create a resource file called StringMessages.resx in your project, and then create a string resource named Hello, you will be able to use the following line of code:

Console.WriteLine(StringMessages.Hello); 


And the type of the resource will be a string, not an object. This is an extremely powerful new feature that dramatically increases productivity for developers working with resources.

The demo in Listing 12.3 consists of a console application called ResourceDemo, and a resources file called DemoResources. To that resource file, a string called Greeting has been added, shown in Figure 12.3.

Figure 12.3. Adding a string to a resource file using the Resource Editor.


A class library called AdditionalResources was then created, and a resource file was added to that project in order to illustrate the right pattern for sharing resources between assemblies. Finally, an XML document was added as a file-type resource to the DemoResources resource file. The code in Listing 12.3 shows how to make use of the strongly typed class wrapper that is generated at compile time for access to embedded resources.

Listing 12.3. Accessing Resources Using the Strongly Typed Wrappers

using System; using System.Xml; using System.Resources; using System.Collections.Generic; using System.Text; namespace ResourceDemo { class Program { static void Main(string[] args) {     Console.WriteLine(DemoResources.Greeting);     Console.WriteLine(AdditionalResources.Class1.WelcomeParagraph) ;     XmlDocument doc = new XmlDocument();     doc.LoadXml(DemoResources.XmlDocument1);     foreach (XmlNode order in doc.SelectNodes("//Order"))     {         Console.WriteLine("Order id {0}, Total ${1}",          order.Attributes["id"].Value, order.Attributes["total"].Value);     }     Console.ReadLine(); } } } 

The WelcomeParagraph string resource in the AdditionalResources class library is exposed as a static property as shown in the following example:

namespace AdditionalResources { public class Class1 { public static string WelcomeParagraph {     get { return AdditionalStrings.WelcomeParagraph; } } } } 


This illustrates a good design pattern: Each assembly should be responsible for determining to what resources it should provide external access. Although it is still possible using Reflection to get these resources, if you control the encapsulation you can control how your resources are used properly.



Microsoft Visual C# 2005 Unleashed
Microsoft Visual C# 2005 Unleashed
ISBN: 0672327767
EAN: 2147483647
Year: 2004
Pages: 298

flylib.com © 2008-2017.
If you may any questions please contact us: flylib@qtcs.net