Groups | Blog | Home
all groups > dotnet general > november 2006 >

dotnet general : DisplayName through Reflection


Charles Bazi
11/30/2006 9:21:54 PM
Hi,

I have some properties using System.ComponentModel.DisplayName

How can I retrieve that value using Reflection ?

Tom Shelton
11/30/2006 11:01:23 PM
[quoted text, click to view]

Look at system.reflection.propertyinfo. That will let you call the property.
You can get the property using one of the Type.GetProperty overloads.

--
Charles Bazi
12/3/2006 6:49:28 PM


I got the DisplayName this way:

private string getDisplayName ( MemberInfo mi ) {

object[] atts = mi.GetCustomAttributes( typeof( DisplayNameAttribute ),
false );
if(atts.Length > 0) {
DisplayNameAttribute da = atts[0] as DisplayNameAttribute;
return da.DisplayName;
}
return mi.Name;
}

Then I use it in my foreach(PropertyInfo p in properties)

mything.label = getDisplayName(p);

Found info here: http://forums.devx.com/showpost.php?p=441665&postcount=2

Thank you

[quoted text, click to view]
AddThis Social Bookmark Button