gpt4 book ai didi

支持字符串资源的 c# 自定义属性不加载本地化资源

转载 作者:行者123 更新时间:2023-11-30 18:46:10 24 4
gpt4 key购买 nike

我有一个包含这个自定义属性的 DLL

[AttributeUsage(AttributeTargets.Class)]
public class MyAttribute: System.Attribute {

private ResourceManager resManager = null;

public MyAttribute() {
}

public Type ResourceType { get; set; }

private string caption = string.Empty;
public string Caption {
get { return getResourceString(caption); }
set { caption = value; }
} //Caption

private string getResourceString(string resourceKey) {

if (string.IsNullOrWhiteSpace(resourceKey))
return string.Empty;

if (ResourceType == default(Type))
return resourceKey;

if (resManager == null)
resManager = new ResourceManager(ResourceType);

return resManager.GetString(resourceKey) ?? string.Format("[[{0}]]", resourceKey);

} //getResourceString()

}

还有一个使用 MyAttribute 的程序 (exe),并且在 Resources.resxResources.fr.resx 中还具有英语和法语所需的资源 (resKeyHello) >分别

[MyAttribute(Caption="resKeyHello", ResourceType=typeof(Resources)]
public Foo() {
}

问题是它从不使用法语卫星程序集。我还尝试像这样创建 ResourceManager ResourceManager(ResourceType.FullName, ResourceType.Assembly),但没有任何效果。并且还尝试像这样加载资源 resManager.GetString(resourceKey, Thread.CurrentThread.CurrentUICulture)

我可以在调试器中看到 CurrentUICulture 是法语的。

最佳答案

我终于找到了解决方法。

问题似乎与法语资源文件的生成方式有关。我使用了项目 -> 新建 -> 具有相同名称和 .fr.resx 扩展名的资源文件。

附属 DLL 是在编译期间在 ./bin/fr 子文件夹中生成的,但运行时没有看到它。

最后我使用了 CodePlex 的“ResX Resource Manager”来生成我的资源文件,现在一切正常

关于支持字符串资源的 c# 自定义属性不加载本地化资源,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30848308/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com