c# - 反射如何在资源存储库上发挥作用

标签 c# .net localization resources

我想了解这样的代码如何检索指定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, the GetString(String, CultureInfo) method uses the current UI culture obtained from the CultureInfo.CurrentUICulture property.

这就是全部 - 只需调用默认使用当前 UI 区域性的库方法。

关于c# - 反射如何在资源存储库上发挥作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25367606/

相关文章:

.net - strings.formatnumber 限制为 15 个字符?

ios - Xcode本地化文件复制不正确

iphone - "WARNING: Input manager failed to load static dictionary for: nl_NL"是什么意思?

javascript - 如何检测JS中的非罗马字符?

c# - 我什么时候应该使用 "Hidden TextBox"什么时候应该使用 (html5) "data-attribute"?

c# - 我如何根据参数的类型做不同的事情(不使用反射)?

c# - 如何使用 RegEx 确定下一个允许使用的字符

c# - WPF 组合框 : Align selected value and/or drop down items

c# - Entity Framework IdentityUser覆盖用户名不会保存在数据库中

c# - IFormFile 拒绝 ASP.Net Core 访问路径