【问题标题】:Getting a string from a resource file in an untyped manner以无类型方式从资源文件中获取字符串
【发布时间】:2011-05-20 20:52:15
【问题描述】:
我正在开发一个 ASP.Net 项目,该项目的所有翻译都在 Translations.resx 文件中。有没有一种简单的方法可以以无类型方式获取翻译后的字符串?
我不想做
Translations.TranslateThisKey
而是类似
Translations["TranslateThisKey"]
我需要这个,因为关键是来自外部资源的代码。
【问题讨论】:
标签:
c#
.net
resources
localization
resx
【解决方案1】:
试试这个
var Translations = new ResourceManager("MyResources",
Assembly.GetExecutingAssembly())
.GetResourceSet(CultureInfo.CurrentCulture, false, true)
.Cast<DictionaryEntry>()
.Where(e => e.Value is string)
.ToDictionary(e => e.Key, e => (string) e.Value);
var result = Translations["TranslateThisKey"];
【解决方案2】:
Resources.ResourceManager.GetString("NAME_OF_YOUR_STRING_IN_RESX_FILE")