【发布时间】:2016-09-23 04:12:06
【问题描述】:
我知道两种从资源文件中获取字符串的方法。
方法一:直接访问。
string str = _NAMESPACE.Properties.Resources.HelloWorld;
方法2:通过ResourceManager。
ResourceManager rm = new ResourceManager("_NAMESPACE.Properties.Resources", Assembly.GetExecutingAssembly());
string str = rm.GetString("HelloWorld");
如果有多个本地化资源文件,这两种方法都会得到正确的本地化字符串。
如果我在字符串键中输入错误,method1 将在构建时失败。 Method2 将失败,直到运行时。因此,我认为method1优于method2。
我在这个论坛上发现了一些类似的问题。但是,我的问题仍然没有得到很好的答案。
Why is it better to call the ResourceManager class as opposed to loading resources directly by name?
方法1真的更好吗?
【问题讨论】:
-
看看第二个代码中所有那些硬编码的字符串!第一个对重构更友好。
-
如果您希望将资源分配到 多个 资源文件(例如,
Properties.Messages、Properties.Icons等),请参阅this question 及其答案。
标签: c# localization