我正在使用反射来加载一个树形视图,该视图显示项目的类结构。每个类中的成员都有一个自定义属性。
我可以使用 MemberInfo.GetCustomAttributes()
获取类的属性,但是我需要一种方法来确定类成员是否是自定义类,并且需要解析本身才能返回自定义属性。
到目前为止,我的代码是:
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.
}
}
有没有一种简单的方法可以获取 MemberInfo 实例的目标类型,以便我可以适当地处理它?我觉得我可能错过了一些显而易见的东西,但我正在打转。