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();
}
}
要调用此扩展方法,
var key = Resources.ResourceManager.GetResourceName(value, CultureInfo.InvariantCulture, true);
在这种情况下,我们不想传递资源程序集,而是可以使用特定资源的 resourceManager 进行调用。