Resource Wrapper

 

Download Source Code

 

This utility is an application that will take a given resource file, read it in and generate a class that “wraps” the resource file.  What do I mean by “wrap”?  I mean it will generate a class that has properties for each resource in that resource file.  For instance if you have a resource called “DiscoveryPark5” that is a Bitmap, then there will be a property named DiscoveryPark5 that is of type Bitmap.  For example:

 

C# Generated Code

private const string DISCOVERYPARK5 = "DiscoveryPark5";

public Bitmap DiscoveryPark5

{

  get

  {

    if (!_isLoaded)

      Init(); 

 

    return (Bitmap)_resources.GetObject(DISCOVERYPARK5);

  }

}

 

View of the resources file

 

This allows you to write code that uses the resources file to look like this:

 

Images imageResource = new Images();

_pbImage.Image = imageResource.DiscoveryPark5;

 

 

In the download there are 3 projects (written in C# VS.Net 2003): ResourceWrapper (dll), RW (exe) and WinResourceWrapper (exe).

 

WinResourceWrapper

This utility (shown below) is a simple Windows Forms application that will generate a wrapper class for a resource file, in VB.Net, C# or Managed C++.

 

 

The Resource file can either be an .resx file or a .resources file.  The language choices are: vb = Visual Basic .Net, cs = C# or cpp = Managed C++.  The namespace is usually going to be the name of your exe.  In the example screenshot I put Henley (code name for my research project).  This will be used in two places: namespace declaration and the resource file name.  As can be seen below, there are some differences in the generated classes depending on the language.

 

C#

namespace Henley

{

      public class Images

      {

 

            #region Member Variables

            // resource file to be used as default

            private const string RESOURCE_FILE = "Henley.Images";

 

VB.Net

 

      Public Class Images

            #Region " Member Variables "

            ' resource file to be used as default

            Private Const RESOURCE_FILE As String = "Henley.Images"

 

C++

namespace Henley

{

      public __gc class Images

      {

 

      private:

            // resource file to be used as default

            static String* RESOURCE_FILE = S"Henley.ResourceFiles";

 

VB.Net usually has a default namespace of the exe, so I didn’t output the Namespace declaration in the template.  In order to get the resx file into a C++ project, I added it in the ResourceFiles section, so the template is built for that scenario.

 

The advantages of WinResourceWrapper are:

  1. You have the code to see how it works
  2. The class templates are just XSLT templates embedded in the ResourceWrapper project, so you can edit them as you need
  3. You will have strongly typed access to your resources

 

RW

This utility has similar functionality as the WinResourceWrapper except it can also output the XML data file (usually for debugging the class templates).  The XML file will be the XML representation of the metadata describing the resource file that gets styled into the wrapper class.  There are currently 4 switches:

 

 

A full example (excluding the –x switch) that would generate a C# class would be:

RW –r:c:\images\appResources.resources –l:cs –n:Henley

 

 

The advantages of RW are:

  1. All the same advantages as WinResourceWrapper
  2. You have the ability to save out the XML data file to help debug editing the code templates

 

Now that the Resource File Creator (http://jasonhaley.com/articles/resourcefiles/resource%20file%20creator.htm) and the Resource Wrapper are complete and somewhat documented, my sample application in the next article will make sense.  I have used both of these utilities to create the resource file and then create a wrapper for that resource file… so I can now assume you know how I did that and concentrate on the more interesting stuff.  The next article will be the big one on Resource Files and include a sample application that is written in C#, VB.Net and managed C++…. Coming soon!