Thursday, May 14, 2015

Define Display Labels as attributes in BE class in C#

Assume that in your BE class you have certain properties to match with table fields. I want to expose descriptive name for each of these properties. for example to show it as column header in grid.
For example, there is a property called FirstName. I want to expose it's descriptive name as First Name
in BE define this.
[DisplayName("First Name"), Description("First Name of the Member")]
public string FirstName
{
    get { return _firstName; }
    set { _firstName = value; }
}
You can read these details of each property as below;
PropertyDescriptorCollection propertiesCol = TypeDescriptor.GetProperties(objectBE);

PropertyDescriptor property;

for (int i = 0; i < propertiesCol.Count; i++)
{
    property = TypeDescriptor.GetProperties(objectBE)[i];

    /*
    // Access the Property Name, Display Name and Description as follows
    property.Name          // Returns "FirstName"
    property.DisplayName   // Returns "First Name"
    property.Description   // Returns "First Name of the Member"
    */
}
* where objectBE is the object instance of BE class.

No comments:

Post a Comment