Static constructors and resources

  • Thread starter Thread starter Wavemaker
  • Start date Start date
W

Wavemaker

I'm writing a class whose methods can throw exceptions under certain
circumstances. These exceptions will need string messages. I was
thinking about placing these messages in a resource instead of hard
coding them into the class itself.

The question I'm pondering is where would be the best place to load
these string resources? The string messages will be the same for
every instance of the class, so I was thinking about loading the
resource in a static constructor, retrieving the messages, and
storing them in class fields. This prevents the resources from being
repeatedly loaded each time a new instance of the class is
instantiated.

Are there any disadvantages to this approach? The only problem I can
think of is dealing with a situation in which the culture is changed
at some point during runtime. The resources would need to be
reloaded at that point. Admittedly, at this point, I am only
supporting one culture, so this may not be an issue, but this could
change in the future.
 
Another approach, that I think is a bit more flexible, is to use an XML file
and store you error messages in it. In this case, you will be able to change
your messages without using resources by simply altering your XML file
though a notepad or other editor. Also, you can create several XML files for
each culture, you may check in your code which culture it is and name XML
file accordingly. In this case you will not need to recompile your code, but
load an XML file on the fly.
 
I like the XML idea, but look also at ResourceManager and GetString.
This may be what you had in mind in the original post. I would just
run the GetString() function as you need it instead of putting it in a
static constructor. Especially if it's only needed for exceptions
(which hopefully aren't occurring very often). Note that GetString is
overloaded to handle diferent CultureInfo's.

# String Table text file
Message1 = This was an error.
Message2 = This was an exceptional error.

then add the file to the assembly as a resource (see online help for
more info). The following lines of code will read it out (assuming
the resource is named StringTable):

ResourceManager stringResources = new ResourceManager("StringTable",
Assembly.GetExecutingAssembly());
string msg = stringResources.GetString("Message1");

-mike
 
First of all, thanks to you and codewriter for your reply.

Michael Mayer said:
I like the XML idea, but look also at ResourceManager and
GetString. This may be what you had in mind in the
original post. I would just run the GetString() function
as you need it instead of putting it in a
static constructor. Especially if it's only needed for exceptions
(which hopefully aren't occurring very often).

I will investigate the XML idea, but the ResourceManager route is
the way I was thinking about doing it. I was just wondering how
intensive it is to load the resource and then extract the string
message. Since it's, as you've pointed out, only for use with
exceptions, which shouldn't occur very often, maybe this shouldn't
be of any concern to me.
 
Back
Top