我想了解这样的代码如何检索指定ThreadUI文化的资源?
var value = resourceAssembly
.GetType("Namespace.FooBar")
.GetProperty("Hello")
.GetValue(null, null) as string;
如果我的 ThreadUI 文化是英语,我会得到英语值。如果是德语,我会得到德语值。 好的。但它内部是如何运作的呢?
最佳答案
如果打开生成的与该类型对应的 C# 文件,您将看到如下内容:
internal static string Hello {
get {
return ResourceManager.GetString("Hello", resourceCulture);
}
}
除非您特定resourceCulture
(通过设置Culture
属性),它仍然是null
,所以上面将相当于:
return ResourceManager.GetString("Hello", null);
ResourceManager
属性返回一个 System.Resources.ResourceManager
,如果您查看文档 ResourceManager.GetString(string, CultureInfo)
你会看到:
In desktop apps, if culture is
null
, theGetString(String, CultureInfo)
method uses the current UI culture obtained from theCultureInfo.CurrentUICulture
property.
这就是全部 - 只需调用默认使用当前 UI 区域性的库方法。
关于c# - 反射如何在资源存储库上发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367606/