Here's how my RESX file look like:
这是我的RESX文件的样子:
Name Value Comments
Rule_seconds seconds seconds
Rule_Sound Sound Sound
What I want is: Name by string Value, something like below:
我想要的是:按字符串名称值,如下所示:
public string GetResxNameByValue(string value)
{
// some code to get name value
}
And implement it like below:
并按如下方式实现:
string str = GetResxNameByValue("seconds");
so that str
will return Rule_seconds
这样str将返回Rule_seconds
Thanks!
6 个解决方案
#1
22
This could work
这可行
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;
}
With some additional error checking..
有一些额外的错误检查..
#2
4
you can access directly by passing key:
您可以通过密钥直接访问:
public string gtresource(string rulename)
{
string value = null;
System.Resources.ResourceManager RM = new System.Resources.ResourceManager("CodedUITestProject1.Resource1", this.GetType().Assembly);
value = RM.GetString(rulename).ToString();
if(value !=null && value !="")
{
return value;
}
else
{
return "";
}
}
#3
2
Just in case it might help anyone. This ResourceHelper is inspired by jure and Mohan Singh Saini.
以防它可以帮助任何人。这个ResourceHelper的灵感来自于jure和Mohan Singh Saini。
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;
public class ResourceHelper
{
/// <summary>
/// ResourceHelper
/// </summary>
/// <param name="resourceName">i.e. "Namespace.ResourceFileName"</param>
/// <param name="assembly">i.e. GetType().Assembly if working on the local assembly</param>
public ResourceHelper(string resourceName, Assembly assembly)
{
ResourceManager = new ResourceManager(resourceName, assembly);
}
private ResourceManager ResourceManager { get; set; }
public string GetResourceName(string value)
{
DictionaryEntry entry = ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true).OfType<DictionaryEntry>().FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString() == value);
return entry.Key.ToString();
}
public string GetResourceValue(string name)
{
string value = ResourceManager.GetString(name);
return !string.IsNullOrEmpty(value) ? value : null;
}
}
#4
0
public static class ResourceManagerHelper
{
public static string GetResourceName(this ResourceManager resourceManager, string value, CultureInfo cultureInfo, bool ignoreCase = false)
{
var comparisonType = ignoreCase ? System.StringComparison.OrdinalIgnoreCase : System.StringComparison.Ordinal;
var entry = resourceManager.GetResourceSet(cultureInfo, true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString().Equals(value, comparisonType));
if (entry.Key == null)
throw new System.Exception("Key and value not found in the resource file");
return entry.Key.ToString();
}
}
To Call this extension method,
要调用此扩展方法,
var key = Resources.ResourceManager.GetResourceName(value, CultureInfo.InvariantCulture, true);
In this case, we don't want to pass the resource assembly, rather we can invoke using the particular resource's resourceManager.
在这种情况下,我们不想传递资源程序集,而是可以使用特定资源的resourceManager调用。
#5
0
You could use this inline code (c#, ASP.NET MVC)
你可以使用这个内联代码(c#,ASP.NET MVC)
var _resource = new ResourceManager(typeof(/*your resource*/));
string res = _resource.GetString(/*resource key*/);
i think this could help u.
我想这可以帮到你。
#6
0
Here is an limited example that:
这是一个有限的例子:
- Uses an resource files in a subfolder
- Uses an different culture than the default one
使用子文件夹中的资源文件
使用与默认文化不同的文化
.
public static string GetLocalizedNameReversed(string value)
{
ResourceManager rm = new ResourceManager($"YourNamespace.YourFolder.YourResourceFileName", assembly);
var entry = rm.GetResourceSet(new CultureInfo("nb-NO"), true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(e => e.Value.ToString().Equals(value, StringComparison.OrdinalIgnoreCase));
return entry.Key.ToString();
}
#1
22
This could work
这可行
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;
}
With some additional error checking..
有一些额外的错误检查..
#2
4
you can access directly by passing key:
您可以通过密钥直接访问:
public string gtresource(string rulename)
{
string value = null;
System.Resources.ResourceManager RM = new System.Resources.ResourceManager("CodedUITestProject1.Resource1", this.GetType().Assembly);
value = RM.GetString(rulename).ToString();
if(value !=null && value !="")
{
return value;
}
else
{
return "";
}
}
#3
2
Just in case it might help anyone. This ResourceHelper is inspired by jure and Mohan Singh Saini.
以防它可以帮助任何人。这个ResourceHelper的灵感来自于jure和Mohan Singh Saini。
using System.Collections;
using System.Linq;
using System.Reflection;
using System.Resources;
using System.Threading;
public class ResourceHelper
{
/// <summary>
/// ResourceHelper
/// </summary>
/// <param name="resourceName">i.e. "Namespace.ResourceFileName"</param>
/// <param name="assembly">i.e. GetType().Assembly if working on the local assembly</param>
public ResourceHelper(string resourceName, Assembly assembly)
{
ResourceManager = new ResourceManager(resourceName, assembly);
}
private ResourceManager ResourceManager { get; set; }
public string GetResourceName(string value)
{
DictionaryEntry entry = ResourceManager.GetResourceSet(Thread.CurrentThread.CurrentCulture, true, true).OfType<DictionaryEntry>().FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString() == value);
return entry.Key.ToString();
}
public string GetResourceValue(string name)
{
string value = ResourceManager.GetString(name);
return !string.IsNullOrEmpty(value) ? value : null;
}
}
#4
0
public static class ResourceManagerHelper
{
public static string GetResourceName(this ResourceManager resourceManager, string value, CultureInfo cultureInfo, bool ignoreCase = false)
{
var comparisonType = ignoreCase ? System.StringComparison.OrdinalIgnoreCase : System.StringComparison.Ordinal;
var entry = resourceManager.GetResourceSet(cultureInfo, true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(dictionaryEntry => dictionaryEntry.Value.ToString().Equals(value, comparisonType));
if (entry.Key == null)
throw new System.Exception("Key and value not found in the resource file");
return entry.Key.ToString();
}
}
To Call this extension method,
要调用此扩展方法,
var key = Resources.ResourceManager.GetResourceName(value, CultureInfo.InvariantCulture, true);
In this case, we don't want to pass the resource assembly, rather we can invoke using the particular resource's resourceManager.
在这种情况下,我们不想传递资源程序集,而是可以使用特定资源的resourceManager调用。
#5
0
You could use this inline code (c#, ASP.NET MVC)
你可以使用这个内联代码(c#,ASP.NET MVC)
var _resource = new ResourceManager(typeof(/*your resource*/));
string res = _resource.GetString(/*resource key*/);
i think this could help u.
我想这可以帮到你。
#6
0
Here is an limited example that:
这是一个有限的例子:
- Uses an resource files in a subfolder
- Uses an different culture than the default one
使用子文件夹中的资源文件
使用与默认文化不同的文化
.
public static string GetLocalizedNameReversed(string value)
{
ResourceManager rm = new ResourceManager($"YourNamespace.YourFolder.YourResourceFileName", assembly);
var entry = rm.GetResourceSet(new CultureInfo("nb-NO"), true, true)
.OfType<DictionaryEntry>()
.FirstOrDefault(e => e.Value.ToString().Equals(value, StringComparison.OrdinalIgnoreCase));
return entry.Key.ToString();
}