This blog will describe some of the learning experiences that I have with .NET, some personal projects that I'm working on, and whatever other topics tickle my fancy.
My Blog Links
Tuesday, August 03, 2004
I just started working on a class library and one of the first things that came up was using a string resource from that assembly rather than a hard-coded string. It's always a good idea to start new projects with localization in mind, that way you don't have to go back and retroactively do it. To that end, I developed a little helper class that lets you find the resources in a class library.
The code is pretty straight-forward. It creates an instance of a ResourceManager based on the name of the .resource section in the assembly and the type of this class, which it uses to locate the assembly. Although this particular class is focused on retrieving strings from the resource file, you can actually retrieve any type of resource: strings, icons, bitmaps, etc. The other interesting attribute of this class is that it's implemented as a Singleton pattern.
You can use this class by calling:
You need to be sure that you've added a Strings.resx file to your project, added a resource string with the specified name ("MyResourceString"), and that you've done a full rebuild on the class library project. The full rebuild is sometimes needed because I've run into a couple of instances when just doing a build doesn't build in the resource file, if the project had been previously built without any resources in it.
I have used similar resource classes in the past, but since I just ran into this requirement again, I thought it would be a good topic to blog about.
Comments: Post a Comment