您的位置:首页 > 其它

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

2004-11-23 16:37 627 查看
 
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:         }

在这里,我找到了问题,原来这里根本就没有实现。我不知道这是为什么。
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: