I'm using reflection to load a treeview with the class structure of a project. Each of the members in a class have a custom attribute assigned to them.
I don't have a problem getting the attributes for a class using MemberInfo.GetCustomAttributes()
however I need a way of working out if a class member is a custom class and then needs parsing itself to return the custom attributes.
So far, my code is:
MemberInfo[] membersInfo = typeof(Project).GetProperties();
foreach (MemberInfo memberInfo in membersInfo)
{
foreach (object attribute in memberInfo.GetCustomAttributes(true))
{
// Get the custom attribute of the class and store on the treeview
if (attribute is ReportAttribute)
{
if (((ReportAttribute)attribute).FriendlyName.Length > 0)
{
treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName });
}
}
// PROBLEM HERE : I need to work out if the object is a specific type
// and then use reflection to get the structure and attributes.
}
}
Is there an easy way of getting the target type of a MemberInfo instance so I can handle it appropriately? I feel I'm missing something obvious but I'm going round in circles at the minute.
GetProperties
returns an array ofPropertyInfo
so you should use that.Then it is simply a matter of using the
PropertyType
property.I think you can get better performance if you carry around this extension method:
Should work for any
MemberInfo
, not justPropertyInfo
. You may avoidMethodInfo
from that list since its not under lying type per se (but return type).In your case:
I wonder why this hasn't been part of BCL by default.