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?
Yes, there is System.ComponentModel.DisplayNameAttribute
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With