DisplayNameAttribute的本地化 [英] Localization of DisplayNameAttribute
问题描述
我要寻找一种方式来定位在PropertyGrid中显示的属性名称。属性的名称可能是重写使用DisplayNameAttribute属性。不幸的是属性不能有非恒定的前pressions。所以我不能使用强类型的资源,例如:
I am looking for a way to localize properties names displayed in a PropertyGrid. The property's name may be "overriden" using the DisplayNameAttribute attribute. Unfortunately attributes can not have non constant expressions. So I can not use strongly typed resources such as:
class Foo
{
[DisplayAttribute(Resources.MyPropertyNameLocalized)] // do not compile
string MyProperty {get; set;}
}
我周围一看,发现了一些建议,从DisplayNameAttribute继承到能够使用的资源。我将结束与code,如:
I had a look around and found some suggestion to inherit from DisplayNameAttribute to be able to use resource. I would end up up with code like:
class Foo
{
[MyLocalizedDisplayAttribute("MyPropertyNameLocalized")] // not strongly typed
string MyProperty {get; set;}
}
不过我失去了强类型资源的好处这绝对不是一件好事。那时,我发现<少时href=\"http://msdn.microsoft.com/en-us/library/microsoft.visualstudio.modeling.design.displaynameresourceattribute.aspx\">DisplayNameResourceAttribute这可能就是我要找的。但它应该是在Microsoft.VisualStudio.Modeling.Design命名空间,我无法找到我应该参照什么来添加对这个命名空间。
However I lose strongly typed resource benefits which is definitely not a good thing. Then I came across DisplayNameResourceAttribute which may be what I'm looking for. But it's supposed to be in Microsoft.VisualStudio.Modeling.Design namespace and I can't find what reference I am supposed to add for this namespace.
任何人都知道,如果有一个更简单的方法来实现显示名称本地化的好方法?或者,如果有作为一种使用微软似乎使用的Visual Studio什么?
Anybody know if there's a easier way to achieve DisplayName localization in a good way ? or if there is as way to use what Microsoft seems to be using for Visual Studio ?
推荐答案
下面是我结束了在一个单独的组件,该解决方案(称为在我的情况通用):
Here is the solution I ended up with in a separate assembly (called "Common" in my case):
[AttributeUsage(AttributeTargets.Class | AttributeTargets.Method | AttributeTargets.Property | AttributeTargets.Event)]
public class DisplayNameLocalizedAttribute : DisplayNameAttribute
{
public DisplayNameLocalizedAttribute(Type resourceManagerProvider, string resourceKey)
: base(Utils.LookupResource(resourceManagerProvider, resourceKey))
{
}
}
与code来查找资源:
with the code to look up the resource:
internal static string LookupResource(Type resourceManagerProvider, string resourceKey)
{
foreach (PropertyInfo staticProperty in resourceManagerProvider.GetProperties(BindingFlags.Static | BindingFlags.NonPublic))
{
if (staticProperty.PropertyType == typeof(System.Resources.ResourceManager))
{
System.Resources.ResourceManager resourceManager = (System.Resources.ResourceManager)staticProperty.GetValue(null, null);
return resourceManager.GetString(resourceKey);
}
}
return resourceKey; // Fallback with the key name
}
典型用法是:
class Foo
{
[Common.DisplayNameLocalized(typeof(Resources.Resource), "CreationDateDisplayName"),
Common.DescriptionLocalized(typeof(Resources.Resource), "CreationDateDescription")]
public DateTime CreationDate
{
get;
set;
}
}
什么是pretty因为我用的资源键文字字符串多难看。采用恒定也就意味着要修改Resources.Designer.cs这可能不是一个好主意。
What is pretty much ugly as I use literal strings for resource key. Using a constant there would mean to modify Resources.Designer.cs which is probably not a good idea.
结论:我不开心的这一说法,但我更不愉快微软谁也不能为这种共同任务提供任何有用
Conclusion: I am not happy with that, but I am even less happy about Microsoft who can't provide anything useful for such a common task.
这篇关于DisplayNameAttribute的本地化的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!