资源中的 DisplayName 属性? [英] DisplayName attribute from Resources?
本文介绍了资源中的 DisplayName 属性?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有一个本地化的应用程序,我想知道是否可以从资源设置某个模型属性的 DisplayName
.
I have a localized application, and I am wondering if it is possible to have the DisplayName
for a certain model property set from a Resource.
我想做这样的事情:
public class MyModel {
[Required]
[DisplayName(Resources.Resources.labelForName)]
public string name{ get; set; }
}
但我不能这样做,正如编译器所说:属性参数必须是属性参数类型的常量表达式、typeof 表达式或数组创建表达式":(
But I can't to it, as the compiler says: "An attribute argument must be a constant expression, typeof expression or array creation expression of an attribute parameter type" :(
有什么解决方法吗?我正在手动输出标签,但验证器输出需要这些标签!
Are there any workarounds? I am outputting labels manually, but I need these for the validator output!
推荐答案
如何编写自定义属性:
public class LocalizedDisplayNameAttribute: DisplayNameAttribute
{
public LocalizedDisplayNameAttribute(string resourceId)
: base(GetMessageFromResource(resourceId))
{ }
private static string GetMessageFromResource(string resourceId)
{
// TODO: Return the string from the resource file
}
}
可以这样使用:
public class MyModel
{
[Required]
[LocalizedDisplayName("labelForName")]
public string Name { get; set; }
}
这篇关于资源中的 DisplayName 属性?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文