是否有用于指定属性“显示名称”的.NET属性?

时间:2010-08-26 23:32:57

标签: c# .net properties attributes

是否有一个属性允许您为类中的属性指定用户友好名称?

例如,假设我有以下课程:

public class Position
{
     public string EmployeeName { get; set; }
     public ContactInfo EmployeeContactInfo { get; set; }
}

我想指定EmployeeName属性的显示名称是“Employee Name”,EmployeeContactInfo属性的显示名称是“Employee Contact Information”。

编写我自己的属性类很容易,我可以这样做:

[PropertyDisplayInfo(DisplayName = "Employee Name")]
public string EmployeeName { get; set; }

但这样的东西已经包含在.NET中吗?

4 个答案:

答案 0 :(得分:13)

答案 1 :(得分:12)

System.ComponentModel.DataAnnotations.DisplayAttribute是比DisplayNameAttribute更好的选择,DisplayAttribute实际上是用于属性网格。如今,.NET世界中的更多组件将会启动并使用Order。它还具有GroupNameShortNameAutoGenerateField等细节,以及在完成自动生成时是否显示属性(使用DisplayAttribute)。

{{1}}也是资源友好的,使其成为本地化的好选择。

答案 2 :(得分:3)

如果您想要进行调试,可能会对DebuggerDisplayAttribute

感兴趣

答案 3 :(得分:0)

在每个属性声明之前放置以下属性:

  //[DisplayName("Your desired human readable field caption ")]
    [DisplayName("ID")]
    public int id { 
        get {return _id;}
        set { SetField(ref _id, value, "id"); } 
    }