I think you can get better performance if you carry around this extension method:
public static Type GetUnderlyingType(this MemberInfo member)
{
switch (member.MemberType)
{
case MemberTypes.Event:
return ((EventInfo)member).EventHandlerType;
case MemberTypes.Field:
return ((FieldInfo)member).FieldType;
case MemberTypes.Method:
return ((MethodInfo)member).ReturnType;
case MemberTypes.Property:
return ((PropertyInfo)member).PropertyType;
default:
throw new ArgumentException
(
"Input MemberInfo must be if type EventInfo, FieldInfo, MethodInfo, or PropertyInfo"
);
}
}
Should work for any MemberInfo
, not just PropertyInfo
. You may avoid MethodInfo
from that list since its not under lying type per se (but return type).
In your case:
foreach (MemberInfo memberInfo in membersInfo)
{
foreach (object attribute in memberInfo.GetCustomAttributes(true))
{
if (attribute is ReportAttribute)
{
if (((ReportAttribute)attribute).FriendlyName.Length > 0)
{
treeItem.Items.Add(new TreeViewItem() { Header = ((ReportAttribute)attribute).FriendlyName });
}
}
//if memberInfo.GetUnderlyingType() == specificType ? proceed...
}
}
I wonder why this hasn't been part of BCL by default.
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…