在C#中强力输入资源文件

Rah*_*han 4 c# embedded-resource strong-typing

我有一个Windows应用程序访问类库项目中的文件.

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_", "")));
        }
    }
}
`
Run Code Online (Sandbox Code Playgroud)

我已ProductName在此示例中手动创建了该属性.是否有更简单的方法来访问资源文件中每行的强类型名称?

Mic*_*ard 5

这可能是您正在寻找的:https://docs.microsoft.com/en-us/dotnet/framework/tools/resgen-exe-resource-file-generator

由于资源属性类型是在运行时确定的,因此您需要一个工具来分析资源文件预编译时间并生成所需的属性.Resgen.exe将执行此操作,但您也可以创建自定义t4脚本或其他内容.

来自文档:

以下命令读取基于XML的输入文件myResources.resx,并写入名为myResources.resources的二进制资源文件.它还生成一个名为MyFile.cs的C#文件,其中包含一个名为MyClass的类,该类包含与输入文件中引用的资源匹配的强类型属性.MyClass类包含在名为Namespace1的名称空间中.

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