c# Custom Attribute with support for string resources does not load localized resources

时间:2015-06-15 14:52:07

标签: c# custom-attributes

I have a DLL containing this custom attibutes

[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()

}

And a program (exe) that use the MyAttribute and also has the resources needed (resKeyHello) for both english and french in Resources.resx and Resources.fr.resx respectively

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

The problem is that it never use the french satellite assembly. I also tried to create the ResourceManager like this ResourceManager(ResourceType.FullName, ResourceType.Assembly), but without any effect. And also tried loading the resource like this resManager.GetString(resourceKey, Thread.CurrentThread.CurrentUICulture)

I can see in debugger that CurrentUICulture is french.

1 个答案:

答案 0 :(得分:-1)

我终于找到了解决方法。

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

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

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