c# - 如何从 .NET 资源 (RESX) 文件中按字符串值获取名称

标签 c# .net resx

这是我的 RESX 文件的样子:

Name            Value        Comments
Rule_seconds    seconds      seconds
Rule_Sound      Sound        Sound

我想要的是: 按字符串值命名,如下所示:

public string GetResxNameByValue(string value)
{
// some code to get name value
}

然后像下面这样实现它:

string str = GetResxNameByValue("seconds");

这样 str 将返回 Rule_seconds

谢谢!

最佳答案

这可以工作

private string GetResxNameByValue(string value)
    {
            System.Resources.ResourceManager rm = new System.Resources.ResourceManager("YourNamespace.YourResxFileName", this.GetType().Assembly);


        var entry=
            rm.GetResourceSet(System.Threading.Thread.CurrentThread.CurrentCulture, true, true)
              .OfType<DictionaryEntry>()
              .FirstOrDefault(e => e.Value.ToString() ==value);

        var key = entry.Key.ToString();
        return key;

    }

一些额外的错误检查..

关于c# - 如何从 .NET 资源 (RESX) 文件中按字符串值获取名称,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16361685/

相关文章:

c# - 如何获得给定月份的每个星期一?

c# - 遍历枚举?

c# - HttpContext.Current.Items 的范围

c# - 迭代 IEnumerable 中的每个项目

c# - 无法从 Resources.resx 获取新添加的文件

c# - 三层文化特定资源文件

c# - PowerShell 和服务器管理器模块的跨平台系统库引用

C# Winform 桌面图标拒绝在特定计算机上更改

c# - 匹配进程参数的正则表达式

c# - 如何将 HTML 代码放入 .resx 资源文件?