5

I have a windows application that accesses a file in a class library project.

public static class Global
{
    private static ResourceManager ResourceManager { get; set; }

    static Global ()
    {
        ResourceManager = new ResourceManager("MyClassLibraryProject.Resource", Assembly.GetExecutingAssembly());
    }

    private static string GetValue (string name)
    {
        return (ResourceManager.GetString(name, Options.CultureInfo));
    }

    public static string ProductName
    {
        get
        {
            return (GetValue(MethodBase.GetCurrentMethod().Name.Replace("get_", "")));
        }
    }
}
`

I have created the ProductName property manually in this example. Is there an easier way to access strongly-typed names for each row in the resource file?

David Gardiner
  • 16,892
  • 20
  • 80
  • 117
Raheel Khan
  • 14,205
  • 13
  • 80
  • 168

1 Answers1

5

This might do what you're looking for: https://learn.microsoft.com/en-us/dotnet/framework/tools/resgen-exe-resource-file-generator

Since resource property types are determined at runtime, you'll need a tool to analyse the resource file pre-compile time and generate the wanted properties. Resgen.exe will do this, but you could create a custom t4 script or something else as well.

From the docs:

The following command reads an XML-based input file myResources.resx and writes a binary resources file named myResources.resources. It also generates a C# file named MyFile.cs with a class named MyClass that contains strongly-typed properties that match the resources that are referenced in the input file. The MyClass class is contained within a namespace named Namespace1.

resgen myResources.resx myResources.resources /str:C#,Namespace1,MyClass,MyFile.cs

Community
  • 1
  • 1
Michael Sondergaard
  • 1,464
  • 10
  • 25
  • Here is what I discivered. Visual Studio 2010 is already generating a class file at build time using the tool `ResXFileCodeGenerator`. The accessibility of the class, however, is `internal` and if I change it, it gets replaced at build time. Is there a way to customize this behavior so I can specify a public access modifier? – Raheel Khan Apr 19 '12 at 06:59
  • 1
    Found it [here](http://stackoverflow.com/questions/4274311/visual-studio-resx-file-default-internal-to-public). Can't believe I missed that. – Raheel Khan Apr 19 '12 at 07:25