It is very common to use resource files in .Net applications when you need to store some data in a XML file. I usually use .resx files as string tables in order to avoid hard coded strings in my code. Two months ago, we published a post about a free tool which helps to extract hard coded strings to resource files. Notice that when you add a resource file, there is an automatically generated class with properties issued from the resources elements, so you can call this class properties instead of using the ResourceManager. The only problem with this class is that its properties are marked as internal and thus can’t be accessed externally (from other assemblies).

Sometimes you want to gather all of the strings in one central assembly because the same string might be used several times by different code components. Think about localization, you want to have one assembly where all of the messages to the user are located. If you try to use those string tables from other assemblies, you will fail because of the internal keyword. Fortunately, there is a way to instruct the generation process to build a public class with public properties:

Visual Studio 2008: Very easy process, just open the resource editor and change the value in the Access Modifier drop down menu from Internal to Public.

image

Visual Studio 2005: There is a free custom tool for that purpose called ResXFilePublicCodeGenerator – Download and install it. After the installation, open the properties window of the resource file and change the Custom Tool to ResXFilePublicCodeGenerator, than right click on the resource file and choose the Run Custom Tool from the drop down menu.

image image

That is it, Do you know other ways to do the job? If so, leave your comment…

Tags :

7 Responses to “How to Generate Public Classes for Resource Files”


  1. Jeffrey

    Said on July 1, 2008 :

    I’ve also tried the custom tool approach, which I found to be too high-friction. Since 2005 does not allow the access modifiers to be changed (it will change them back whenever the resx designer is used, even if you manually modify them), I’ve resorted to using the InternalsVisibleTo attribute in the AssemblyInfo of the project that contains the resources.

    Example: [assembly: InternalsVisibleTo(“Assembly.That.Needs.To.See.Me”)]

    I just make sure to create a separate project just for my resources, so that is the only thing that gets its internals exposed.

  2. Ray

    Said on July 2, 2008 :

    I’ve used the InternalsVisibleTo attribute with VS2005 – http://initializecomponent.blogspot.com/2008/06/stupid-net-tricks-1.html

  3. Paul

    Said on July 3, 2008 :

    I stumbled upon this through http://www.dotnetkicks.com and it solved an outstanding enhancement request I’ve had on my plate for a year. Didn’t know they’d added this feature in VS2008. Thanks!

    I’ve kicked the story and checked out an AdSense ad … enjoy your quarter. :)

  4. garryu2

    Said on July 28, 2008 :

    Finally. Anyone has an idea why it was so hard do include this in VS2005?

  5. Rick

    Said on November 17, 2008 :

    Awesome.

    Helped me alot.

  6. Chuck

    Said on September 15, 2010 :

    Can you put up an example of using these classes?

    I have an xml file and am trying to access it via some form of XElement.Load()

1 Trackback(s)

  1. Jul 2, 2008: Visual Studio Links #45 : Visual Studio Hacks

Post a Comment