我有一个 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_", "")));
}
}
}
`
在此示例中,我手动创建了 ProductName
属性。有没有更简单的方法来访问资源文件中每一行的强类型名称?
最佳答案
这可能会满足您的需求:https://learn.microsoft.com/en-us/dotnet/framework/tools/resgen-exe-resource-file-generator
由于资源属性类型是在运行时确定的,因此您需要一个工具来分析资源文件的预编译时间并生成所需的属性。 Resgen.exe 会执行此操作,但您也可以创建自定义 t4 脚本或其他脚本。
来自文档:
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
关于c# - C#中的强类型资源文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10208152/