Re: Enforcing (forcing) Resource Strings

Tech-Archive recommends: Fix windows errors by optimizing your registry



* Alan wrote, On 28-6-2007 21:56:
Is there a way in C# to have the compiler enforce that users use
Resource Strings?

Say I have a logging subsystem, and I want make sure that the only
string values that get logged come from a resource file, can I do
that?

I know I can use:

static void LogWarning(string val)
{
...
string toLog = ResourceManager.GetString(val);
}

But that is problematic: At compile time, I have no idea if val
exists.

I can also use:

static void LogWarning(string msg)
{
...
}

LogWarning(ErrorStrings.ThisIsMyError);

But nothing stops users of this log system from passing in a hardcoded
string into my LogWarning method.

Ideally I want something that is strongly typed, but can't be
overloaded with a string.

Any ideas?


There is an FxCop rule to enforce this, if you're using Visual Studio Team System Developer or Suite you can enforce these rules before check-ins.

There is no way however to make every string parameter unsuitable for a strings. You can make a resource file type safe in .NET 2.0. If you open a resource file and look in the information/message tab there should be a link there to generate a typed class for that specific resource file.

If you're still using .NET 1.1 you can use the String Resource Tool. But I don't know where it moved to with the close down of Got Dot Net. Here's a post about it:

http://blogs.msdn.com/scottdensmore/archive/2005/07/08/436867.aspx

I hope this helps,

Jesse
.



Relevant Pages

  • [API] Win32 Resources - List Icon Resource Names
    ... private static extern IntPtr LoadLibraryEx(string FileName, ... /// Gets the string resource names for the current file. ...
    (microsoft.public.dotnet.languages.csharp)
  • Re: UpdateResource being weird
    ... > That's funny, I was working on updateresource just yesterday, only i was ... > for String Types. ... >> inspection using a resource viewer shows that the MSI file is being ... but the INI file is messing me arround. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: How to add button on Mail editor which is not outlook.??
    ... 'The bmp files are added to a resource file. ... Private Declare Function GdiplusShutdown Lib "GDIPlus" (_ ... ByVal filename As String) As StdPicture ... Dim tSI As GdiplusStartupInput ...
    (microsoft.public.office.developer.outlook.vba)
  • Re: Other version information
    ... header to locate the resource table. ... Windows PE files ... marked by a literal string. ... files in a folder, ...
    (microsoft.public.scripting.vbscript)
  • Re: Loading string resource from a file
    ... But how do i use the TypeSafe wrapper where is this? ... Studio 05 projects can create a resource file directly for a project (in ... This assumes all string resources are in a separate assembly called ...
    (microsoft.public.dotnet.framework.compactframework)