使用reflection获取MemberInfo的类型
我正在使用reflection来加载具有项目类结构的树视图。 类中的每个成员都有一个分配给它们的自定义属性。
我使用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实例的目标类型,以便我可以适当地处理它? 我觉得我错过了一些显而易见的东西,但我现在正在转圈。
GetProperties
返回一个PropertyInfo
数组,因此您应该使用它。
然后,只需使用PropertyType
属性即可。
PropertyInfo[] propertyInfos = typeof(Project).GetProperties(); foreach (PropertyInfo propertyInfo in propertyInfos) { // ... if(propertyInfo.PropertyType == typeof(MyCustomClass)) // ... }
我认为如果你随身携带这种扩展方法,你可以获得更好的性能:
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" ); } }
应该适用于任何MemberInfo
,而不仅仅是PropertyInfo
。 您可以从该列表中避免使用MethodInfo
,因为它本身不是谎言类型(但返回类型)。
在你的情况下:
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... } }
我想知道为什么默认情况下这不是BCL的一部分。
上述就是C#学习教程:使用reflection获取MemberInfo的类型分享的全部内容,如果对大家有所用处且需要了解更多关于C#学习教程,希望大家多多关注—计算机技术网(www.ctvol.com)!
本文来自网络收集,不代表计算机技术网立场,如涉及侵权请联系管理员删除。
ctvol管理联系方式QQ:251552304
本文章地址:https://www.ctvol.com/cdevelopment/1007523.html