views:

104

answers:

4

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?

+7  A: 

Yes, there is System.ComponentModel.DisplayNameAttribute

Thomas Levesque
as of .NET 4, there is a better attribute for this, System.ComponentModel.DataAnnotations.DisplayAttribute
Matt Greer
A: 

Why don't you use resource files? They support multiple language (may need it in future) and you can seperate your code from things it should not know about.

Zebi
+6  A: 

System.ComponentModel.DataAnnotations.DisplayAttribute is a better choice than DisplayNameAttribute, which is actually intended for use in property grids. Now a days 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.

Matt Greer
+2  A: 

If you want this for debugging, you might be interested in the DebuggerDisplayAttribute.

Douglas