c# - C#中的强类型资源文件

标签 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_", "")));
        }
    }
}
`

在此示例中,我手动创建了 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/

相关文章:

c# - 当每个项目可以有多个键时,是否可以对集合进行分组?

c# - 使用嵌入式 DLL?

java - 我应该在哪里放置图像以使用它来创建新的 Swing ImageIcon 对象?

python - Python 是强类型的吗?

c# - UpdatePanel 中的按钮触发事件,但页面没有更新

c# - Windows 商店应用程序 : Peer to peer connection

arrays - JSON.NET 无法处理简单的数组反序列化?

c - C是强类型的吗?

c# - MVVM – UWP 的层次结构和导航隐式绑定(bind)

java - 如何将图像包含到可执行的 .jar 中?