是否有一个属性允许您为类中的属性指定用户友好名称?
例如,假设我有以下课程:
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中吗?
答案 0 :(得分:13)
答案 1 :(得分:12)
System.ComponentModel.DataAnnotations.DisplayAttribute
是比DisplayNameAttribute
更好的选择,DisplayAttribute
实际上是用于属性网格。如今,.NET世界中的更多组件将会启动并使用Order
。它还具有GroupName
,ShortName
,AutoGenerateField
等细节,以及在完成自动生成时是否显示属性(使用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"); }
}