Attribute.GetCustomAttribute Method (MemberInfo, Type, Boolean)不能返回继承类的特性?

 

Attribute.GetCustomAttribute Method (MemberInfo, Type, Boolean)不能返回继承类的特性

MSDN文档里面,对这个方法的说明是:
Retrieves a custom attribute of a specified type applied to a specified member of a class or optionally inherited from a base class.
就是说,可以返回MemberInfo的继承成员的特性。

可是当我想利用这个便利的时候,才发现我被MSDN文档骗了。

我开始查找资料,从这个方法开始往下搜索:

656:         public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType, bool inherit)
657:         {
658:             Attribute[] attrib = GetCustomAttributes(element,attributeType,inherit);
659:
660:             if (attrib.Length == 0)
661:                 return null;
662:
663:             if (attrib.Length == 1)
664:                 return attrib[0];
665:             else
666:               throw new AmbiguousMatchException(Environment.GetResourceString("RFLCT.AmbigCust"));
667:         }

48:         public static Attribute[] GetCustomAttributes(MemberInfo element, Type type, bool inherit)
49:         {
50:             if (element == null)
51:                 throw new ArgumentNullException("element");
52:
53:             if (type == null)
54:                 throw new ArgumentNullException("type");
55:             
56:             if (!type.IsSubclassOf(typeof(Attribute)) && type != typeof(Attribute))
57:                 throw new ArgumentException(Environment.GetResourceString("Argument_MustHaveAttributeBaseClass"));
58:
59:             Object[] attributes = null;
60:             switch(element.MemberType)
61:             {
62:                 case MemberTypes.Method:
63:                     attributes = ((MethodInfo)element).GetCustomAttributes(type, inherit);
64:                     break;
65:
66:                 case MemberTypes.TypeInfo:
67:                 case MemberTypes.NestedType:
68:                     attributes = ((Type)element).GetCustomAttributes(type, inherit);
69:                     break;
70:
71:                 case MemberTypes.Constructor:
72:                     attributes = ((ConstructorInfo)element).GetCustomAttributes(type, inherit);
73:                     break;
74:                     
75:                 case MemberTypes.Field:
76:                     attributes = ((FieldInfo)element).GetCustomAttributes(type, inherit);
77:                     break;
78:
79:                 case MemberTypes.Property:
80:                     return InternalGetCustomAttributes((PropertyInfo)element, type, inherit);
81:
82:                 case MemberTypes.Event:
83:                     return InternalGetCustomAttributes((EventInfo)element, type, inherit);
84:
85:                 default:
86:                     throw new NotSupportedException(Environment.GetResourceString("NotSupported_UnsupportedMemberInfoTypes"));
87:             }
88:             return (Attribute[])attributes;
89:         }

135:         private static Attribute[] InternalGetCustomAttributes(PropertyInfo element, Type type, bool inherit)
136:         {
137:             // walk up the hierarchy chain
138:             Attribute[] attributes = (Attribute[])element.GetCustomAttributes(type, inherit);
139:             if (inherit) {
140:                 // create the hashtable that keeps track of inherited types
141:                 Hashtable types = new Hashtable(11);
142:                 // create an array list to collect all the requested attibutes
143:                 ArrayList attributeList = new ArrayList();
144:                 CopyToArrayList(attributeList, attributes, types);
145:
146:                 PropertyInfo baseProp = GetParentDefinition(element);
147:                 while (baseProp != null) {
148:                     attributes = GetCustomAttributes(baseProp, type, false);
149:                     AddAttributesToList(attributeList, attributes, types);
150:                     baseProp = GetParentDefinition(baseProp);
151:                 }
152:                 return (Attribute[])attributeList.ToArray(type);
153:             }
154:             else
155:                 return attributes;
156:         }

817:         static private PropertyInfo GetParentDefinition(PropertyInfo property) {
818:             // for the current property get the base class of the getter and the setter, they might be different
819:             MethodInfo propAccessor = property.GetGetMethod(true);
820:             if (propAccessor == null)
821:                 propAccessor = property.GetSetMethod(true);
822:             if (propAccessor != null) {
823:                 propAccessor = propAccessor.GetParentDefinition();
824:                 if (propAccessor != null)
825:                     return propAccessor.DeclaringType.GetProperty(property.Name, property.PropertyType);
826:             }
827:             return null;
828:         }

从这里开始转到MethodInfo

68:         internal virtual MethodInfo GetParentDefinition()
69:         {
70:             return null;
71:         }

在这里,我找到了问题,原来这里根本就没有实现。我不知道这是为什么。

转载于:https://www.cnblogs.com/mahope/archive/2004/11/23/67623.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值