Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a .NET attribute for specifying the "display name" of a property?

Is there a property that allows you to specify a user friendly name for a property in a class?

For example, say I have the following class:

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

I'd like to specify that the display name for the EmployeeName property is "Employee Name" and the display name for the EmployeeContactInfo property is "Employee Contact Information".

It's easy enough to write my own attribute class that allows me to do this:

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

But is something like this already included in .NET?

like image 684
Robert Hardy Avatar asked Aug 26 '10 23:08

Robert Hardy


2 Answers

Yes, there is System.ComponentModel.DisplayNameAttribute

like image 117
Thomas Levesque Avatar answered Oct 19 '22 16:10

Thomas Levesque


System.ComponentModel.DataAnnotations.DisplayAttribute is a better choice than DisplayNameAttribute, which is actually intended for use in property grids. Nowadays more components in the .NET world will pick up on and use DisplayAttribute. It also has niceties like Order, GroupName, ShortName and whether to display the property at all, when auto generation is done (with AutoGenerateField).

DisplayAttribute is also resource friendly, making it a good choice for localization.

like image 42
Matt Greer Avatar answered Oct 19 '22 16:10

Matt Greer