Tuesday 28 August 2007

Generating Public, Strongly-Typed Resource Classes with Visual Studio

Visual Studio 2005 does a great job of managing your .resx files and automatically generates strongly-typed classes exposing the contents of those files. If you create a new resource file and add it to your project you'll notice the Custom Tool property has a value of ResXFileCodeGenerator to suppor this behaviour.

This is generally all well and good but there is a catch: ResXFileCodeGenerator generates classes with members marked internal; in other words, you won't be able to access your resources using the generated class if you're working in another project (i.e. another assembly).

The resgen.exe tool does all the hard work behind the scenes and does have a flag called PublicClass that will override this behaviour--set this flag and your classes will be generated with public visibility. Unfortunately you can't run this tool automatically until compile-time, which means your resources won't be as conveniently accessible as they are by default; you'll also have to write a post-build script or use another method to do all the hard work moving your generated files around.

Luckily Visual Studio 2008 solves this problem by allowing you to set the Custom Tool property to PublicResXFileCodeGenerator. As the name suggests, the generated methods come out the other end marked as public and this all happens from within Visual Studio.

If you really can't wait for Visual Studio 2008 (and it's not far away), you may want to look into a handy little extension called ResXFileCodeGeneratorEx. In addition to allowing you to create publically-accessible, strongly-typed classes for your resources, it also helps out if you're dealing with format strings in your resource files. The only downsides I can think of are the fact that Visual Studio 2008 will make this tool less necessary (apart from the format bit) and that every developer will need to install it on their machine. No biggie but the sort of thing that can cause headaches for new developers joining your team.


  1. I just installed VS2008 and was disappointed to find out that the ResXFileCodeGeneratorEx plugin is not recognized. The good news is the source code is available, so my next task is to get it running in VS2008. I have tried the PublicResxGenerator, but without the string formatting/argument options, it doesn't compare to Dmytro's.

  2. Hi rhelliwell,
    I'm still using ResXFileCodeGenertorEx quite happily in VS2008. Both that tool and VS2005 were previously installed on my machine before I installed VS2008. The CustomTool setting on all of my .resx files are still set to ResXFileCodeGenertorEx and after inspecting my satellite assemblies I can confirm they're all publicly accessible.


Spam comments will be deleted

Note: only a member of this blog may post a comment.