DisplayNameAttribute 的本地化
问题说明
我正在寻找一种方法来本地化 PropertyGrid 中显示的属性名称.可以使用 DisplayNameAttribute 属性覆盖"属性的名称.不幸的是,属性不能有非常量表达式.所以我不能使用强类型资源,例如:
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 继承以便能够使用资源的建议.我最终会得到如下代码:
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;}
}
然而,我失去了强类型资源的好处,这绝对不是一件好事.然后我遇到了 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.
有人知道是否有更简单的方法以好的方式实现 DisplayName 本地化?或者是否有使用 Microsoft 似乎用于 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 ?
正确答案
这是我在一个单独的程序集中最终得到的解决方案(在我的例子中称为Common"):
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))
{
}
}
用代码查找资源:
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;
}
}
当我使用文字字符串作为资源键时,这非常难看.使用常量意味着修改 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.
这篇好文章是转载于:学新通技术网
- 版权申明: 本站部分内容来自互联网,仅供学习及演示用,请勿用于商业和其他非法用途。如果侵犯了您的权益请与我们联系,请提供相关证据及您的身份证明,我们将在收到邮件后48小时内删除。
- 本站站名: 学新通技术网
- 本文地址: /reply/detail/tanhcfhgig
-
YouTube API 不能在 iOS (iPhone/iPad) 工作,但在桌面浏览器工作正常?
it1352 07-30 -
iPhone,一张图像叠加到另一张图像上以创建要保存的新图像?(水印)
it1352 07-17 -
保持在后台运行的 iPhone 应用程序完全可操作
it1352 07-25 -
使用 iPhone 进行移动设备管理
it1352 07-23 -
在android同时打开手电筒和前置摄像头
it1352 09-28 -
扫描 NFC 标签时是否可以启动应用程序?
it1352 08-02 -
检查邮件是否发送成功
it1352 07-25 -
Android微调工具-删除当前选择
it1352 06-20 -
希伯来语的空格句子标记化错误
it1352 06-22 -
Android App 和三星 Galaxy S4 不兼容
it1352 07-20