2 // System.Attribute.cs
\r
5 // Miguel de Icaza (miguel@ximian.com) - Original
\r
6 // Nick D. Drochak II (ndrochak@gol.com) - Implemented most of the guts
\r
8 // (C) Ximian, Inc. http://www.ximian.com
\r
14 public abstract class Attribute {
\r
16 protected Attribute ()
\r
20 public virtual object TypeId {
\r
22 // Derived classes should override this default behaviour as appropriate
\r
23 return this.GetType();
\r
27 private static void CheckParameters(object element, Type attributeType)
\r
29 // neither parameter is allowed to be null
\r
30 if (null == element)
\r
32 throw new ArgumentNullException("element");
\r
35 if (null == attributeType)
\r
37 throw new ArgumentNullException("attributeType");
\r
42 private static System.Attribute FindAttribute(object[] attributes)
\r
44 // if there exists more than one attribute of the given type, throw an exception
\r
45 if (attributes.Length > 1)
\r
47 throw new System.Reflection.AmbiguousMatchException("<element> has more than one attribute of type <attributeType>");
\r
50 if (attributes.Length < 1)
\r
52 return (System.Attribute) null;
\r
55 // tested above for '> 1' and and '< 1', so only '== 1' is left, i.e. we found the attribute
\r
56 return (System.Attribute) attributes[0];
\r
59 private static void CheckAncestry(Type attributeType)
\r
61 // attributeType must be derived from type System.Attribute
\r
62 Type t = typeof(System.Attribute);
\r
64 /* fixme: thgi does not work for target monolib2
\r
65 if (!attributeType.IsSubclassOf(t))
\r
67 throw new ArgumentException("Parameter is not a type derived from System.Attribute", "attributeType");
\r
72 public static Attribute GetCustomAttribute(System.Reflection.ParameterInfo element, Type attributeType){
\r
73 return GetCustomAttribute(element, attributeType, true);
\r
76 public static Attribute GetCustomAttribute(System.Reflection.MemberInfo element, Type attributeType){
\r
77 return GetCustomAttribute(element, attributeType, true);
\r
80 public static Attribute GetCustomAttribute(System.Reflection.Assembly element, Type attributeType){ return GetCustomAttribute(element, attributeType, true);
\r
83 public static Attribute GetCustomAttribute(System.Reflection.Module element, Type attributeType){
\r
84 return GetCustomAttribute(element, attributeType, true);
\r
87 public static Attribute GetCustomAttribute(System.Reflection.Module element, Type attributeType, bool inherit){
\r
88 // neither parameter is allowed to be null
\r
89 CheckParameters(element, attributeType);
\r
91 // attributeType must be derived from type System.Attribute
\r
92 CheckAncestry(attributeType);
\r
94 // Module inheritance hierarchies CAN NOT be searched for attributes, so the second
\r
95 // parameter of GetCustomAttributes() is INGNORED.
\r
96 object[] attributes = element.GetCustomAttributes(attributeType, inherit);
\r
98 return FindAttribute(attributes);
\r
101 public static Attribute GetCustomAttribute(System.Reflection.Assembly element, Type attributeType, bool inherit){
\r
102 // neither parameter is allowed to be null
\r
103 CheckParameters(element, attributeType);
\r
105 // attributeType must be derived from type System.Attribute
\r
106 CheckAncestry(attributeType);
\r
108 // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second
\r
109 // parameter of GetCustomAttributes() is INGNORED.
\r
110 object[] attributes = element.GetCustomAttributes(attributeType, inherit);
\r
112 return FindAttribute(attributes);
\r
114 public static Attribute GetCustomAttribute(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){
\r
115 // neither parameter is allowed to be null
\r
116 CheckParameters(element, attributeType);
\r
118 // attributeType must be derived from type System.Attribute
\r
119 CheckAncestry(attributeType);
\r
121 // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second
\r
122 // parameter of GetCustomAttributes() is INGNORED.
\r
123 object[] attributes = element.GetCustomAttributes(attributeType, inherit);
\r
125 return FindAttribute(attributes);
\r
127 public static Attribute GetCustomAttribute(System.Reflection.MemberInfo element, Type attributeType, bool inherit){
\r
128 // neither parameter is allowed to be null
\r
129 CheckParameters(element, attributeType);
\r
131 // attributeType must be derived from type System.Attribute
\r
132 CheckAncestry(attributeType);
\r
134 // MemberInfo inheritance hierarchies can be searched for attributes, so the second
\r
135 // parameter of GetCustomAttributes() is respected.
\r
136 object[] attributes = element.GetCustomAttributes(attributeType, inherit);
\r
138 return FindAttribute(attributes);
\r
141 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element){
\r
142 return System.Attribute.GetCustomAttributes(element, true);
\r
144 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element){
\r
145 return System.Attribute.GetCustomAttributes(element, true);
\r
147 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element){
\r
148 return System.Attribute.GetCustomAttributes(element, true);
\r
150 public static Attribute[] GetCustomAttributes(System.Reflection.Module element){
\r
151 return System.Attribute.GetCustomAttributes(element, true);
\r
153 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, Type attributeType){
\r
154 return System.Attribute.GetCustomAttributes(element, attributeType, true);
\r
156 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, Type attributeType){
\r
157 return System.Attribute.GetCustomAttributes(element, attributeType, true);
\r
159 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, Type attributeType){
\r
160 return System.Attribute.GetCustomAttributes(element, attributeType, true);
\r
162 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, Type attributeType)
\r
164 return System.Attribute.GetCustomAttributes(element, attributeType, true);
\r
166 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, Type attributeType, bool inherit)
\r
168 // element parameter is not allowed to be null
\r
169 CheckParameters(element, attributeType);
\r
171 // make a properly typed array to return containing the custom attributes
\r
172 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);
\r
176 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){
\r
177 // element parameter is not allowed to be null
\r
178 CheckParameters(element, attributeType);
\r
180 // make a properly typed array to return containing the custom attributes
\r
181 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);
\r
185 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, Type attributeType, bool inherit){
\r
186 // element parameter is not allowed to be null
\r
187 CheckParameters(element, attributeType);
\r
189 // make a properly typed array to return containing the custom attributes
\r
190 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);
\r
194 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, Type attributeType, bool inherit)
\r
196 // element parameter is not allowed to be null
\r
197 CheckParameters(element, attributeType);
\r
199 // make a properly typed array to return containing the custom attributes
\r
200 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(attributeType, inherit);
\r
204 public static Attribute[] GetCustomAttributes(System.Reflection.Module element, bool inherit)
\r
206 // element parameter is not allowed to be null
\r
207 CheckParameters(element, typeof(System.Attribute));
\r
209 // make a properly typed array to return containing the custom attributes
\r
210 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);
\r
214 public static Attribute[] GetCustomAttributes(System.Reflection.Assembly element, bool inherit){
\r
215 // element parameter is not allowed to be null
\r
216 CheckParameters(element, typeof(System.Attribute));
\r
218 // make a properly typed array to return containing the custom attributes
\r
219 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);
\r
223 public static Attribute[] GetCustomAttributes(System.Reflection.MemberInfo element, bool inherit){
\r
224 // element parameter is not allowed to be null
\r
225 CheckParameters(element, typeof(System.Attribute));
\r
227 // make a properly typed array to return containing the custom attributes
\r
228 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);
\r
232 public static Attribute[] GetCustomAttributes(System.Reflection.ParameterInfo element, bool inherit){
\r
233 // element parameter is not allowed to be null
\r
234 CheckParameters(element, typeof(System.Attribute));
\r
236 // make a properly typed array to return containing the custom attributes
\r
237 System.Attribute[] attributes = (System.Attribute[]) element.GetCustomAttributes(inherit);
\r
242 public override int GetHashCode(){
\r
243 // TODO: Implement me
\r
247 public virtual bool IsDefaultAttribute(){
\r
248 // Derived classes should override this default behaviour as appropriate
\r
252 public static bool IsDefined(System.Reflection.Module element, Type attributeType){
\r
253 return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);
\r
255 public static bool IsDefined(System.Reflection.ParameterInfo element, Type attributeType){
\r
256 return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);
\r
258 public static bool IsDefined(System.Reflection.MemberInfo element, Type attributeType){
\r
259 return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);
\r
261 public static bool IsDefined(System.Reflection.Assembly element, Type attributeType){
\r
262 return (System.Attribute.GetCustomAttributes(element, attributeType).Length > 0);
\r
264 public static bool IsDefined(System.Reflection.MemberInfo element, Type attributeType, bool inherit){
\r
265 return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);
\r
267 public static bool IsDefined(System.Reflection.Assembly element, Type attributeType, bool inherit){
\r
268 return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);
\r
270 public static bool IsDefined(System.Reflection.Module element, Type attributeType, bool inherit){
\r
271 return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);
\r
273 public static bool IsDefined(System.Reflection.ParameterInfo element, Type attributeType, bool inherit){
\r
274 return (System.Attribute.GetCustomAttributes(element, attributeType, inherit).Length > 0);
\r
277 public virtual bool Match(object obj){
\r
278 // default action is the same as Equals. Derived classes should override as appropriate
\r
279 return this.Equals(obj);
\r