5 // Miguel de Icaza (miguel@ximian.com) - Original
6 // Nick D. Drochak II (ndrochak@gol.com) - Implemented most of the guts
7 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
9 // (C) 2002, 2003 Ximian, Inc. http://www.ximian.com
12 using System.Reflection;
16 [AttributeUsage (AttributeTargets.All)]
18 public abstract class Attribute
20 protected Attribute ()
24 public virtual object TypeId {
26 // Derived classes should override this default behaviour as appropriate
27 return this.GetType ();
31 private static void CheckParameters (object element, Type attributeType)
33 // neither parameter is allowed to be null
35 throw new ArgumentNullException ("element");
37 if (attributeType == null)
38 throw new ArgumentNullException ("attributeType");
40 if (!typeof (Attribute).IsAssignableFrom (attributeType))
41 throw new ArgumentException (Locale.GetText (
42 "Type is not derived from System.Attribute."), "attributeType");
45 private static Attribute FindAttribute (object[] attributes)
47 // if there exists more than one attribute of the given type, throw an exception
48 if (attributes.Length > 1) {
49 throw new AmbiguousMatchException (Locale.GetText (
50 "<element> has more than one attribute of type <attribute_type>"));
53 if (attributes.Length < 1)
56 // tested above for '> 1' and and '< 1', so only '== 1' is left,
57 // i.e. we found the attribute
58 return (Attribute) attributes[0];
61 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType)
63 return GetCustomAttribute (element, attributeType, true);
66 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType)
68 return GetCustomAttribute (element, attributeType, true);
71 public static Attribute GetCustomAttribute (Assembly element, Type attributeType)
73 return GetCustomAttribute (element, attributeType, true);
76 public static Attribute GetCustomAttribute (Module element, Type attributeType)
78 return GetCustomAttribute (element, attributeType, true);
81 public static Attribute GetCustomAttribute (Module element, Type attributeType, bool inherit)
83 // neither parameter is allowed to be null
84 CheckParameters (element, attributeType);
86 // Module inheritance hierarchies CAN NOT be searched for attributes, so the second
87 // parameter of GetCustomAttributes () is IGNORED.
88 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
90 return FindAttribute (attributes);
93 public static Attribute GetCustomAttribute (Assembly element, Type attributeType, bool inherit)
95 // neither parameter is allowed to be null
96 CheckParameters (element, attributeType);
98 // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second
99 // parameter of GetCustomAttributes () is IGNORED.
100 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
102 return FindAttribute (attributes);
105 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType, bool inherit)
107 // neither parameter is allowed to be null
108 CheckParameters (element, attributeType);
110 // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second
111 // parameter of GetCustomAttributes () is IGNORED.
112 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
114 return FindAttribute (attributes);
117 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType, bool inherit)
119 // neither parameter is allowed to be null
120 CheckParameters (element, attributeType);
122 // MemberInfo inheritance hierarchies can be searched for attributes, so the second
123 // parameter of GetCustomAttribute () is respected.
124 return MonoCustomAttrs.GetCustomAttribute (element, attributeType, inherit);
127 public static Attribute[] GetCustomAttributes (Assembly element)
129 return GetCustomAttributes (element, true);
132 public static Attribute[] GetCustomAttributes (ParameterInfo element)
134 return GetCustomAttributes (element, true);
137 public static Attribute[] GetCustomAttributes (MemberInfo element)
139 return GetCustomAttributes (element, true);
142 public static Attribute[] GetCustomAttributes (Module element)
144 return GetCustomAttributes (element, true);
147 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType)
149 return GetCustomAttributes (element, attributeType, true);
152 public static Attribute[] GetCustomAttributes (Module element, Type attributeType)
154 return GetCustomAttributes (element, attributeType, true);
157 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType)
159 return GetCustomAttributes (element, attributeType, true);
162 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType)
164 return GetCustomAttributes (element, attributeType, true);
167 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit)
169 // element parameter is not allowed to be null
170 CheckParameters (element, attributeType);
172 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
175 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit)
177 // element parameter is not allowed to be null
178 CheckParameters (element, attributeType);
180 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
183 public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit)
185 // element parameter is not allowed to be null
186 CheckParameters (element, attributeType);
188 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
191 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit)
193 // element parameter is not allowed to be null
194 CheckParameters (element, attributeType);
196 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
199 public static Attribute[] GetCustomAttributes (Module element, bool inherit)
201 // element parameter is not allowed to be null
202 CheckParameters (element, typeof (Attribute));
204 return (Attribute []) element.GetCustomAttributes (inherit);
207 public static Attribute[] GetCustomAttributes (Assembly element, bool inherit)
209 // element parameter is not allowed to be null
210 CheckParameters (element, typeof (Attribute));
212 return (Attribute []) element.GetCustomAttributes (inherit);
215 public static Attribute[] GetCustomAttributes (MemberInfo element, bool inherit)
217 // element parameter is not allowed to be null
218 CheckParameters (element, typeof (Attribute));
220 return (Attribute []) element.GetCustomAttributes (inherit);
223 public static Attribute[] GetCustomAttributes (ParameterInfo element, bool inherit)
225 // element parameter is not allowed to be null
226 CheckParameters (element, typeof (Attribute));
228 return (Attribute []) element.GetCustomAttributes (inherit);
231 public override int GetHashCode ()
233 return base.GetHashCode ();
236 public virtual bool IsDefaultAttribute ()
238 // Derived classes should override this default behaviour as appropriate
242 public static bool IsDefined (Module element, Type attributeType)
244 return IsDefined (element, attributeType, false);
247 public static bool IsDefined (ParameterInfo element, Type attributeType)
249 return IsDefined (element, attributeType, true);
252 public static bool IsDefined (MemberInfo element, Type attributeType)
254 return IsDefined (element, attributeType, true);
257 public static bool IsDefined (Assembly element, Type attributeType)
259 return IsDefined (element, attributeType, true);
262 public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit)
264 CheckParameters (element, attributeType);
266 MemberTypes mtype = element.MemberType;
267 if (mtype != MemberTypes.Constructor && mtype != MemberTypes.Event &&
268 mtype != MemberTypes.Field && mtype != MemberTypes.Method &&
269 mtype != MemberTypes.Property && mtype != MemberTypes.TypeInfo &&
270 mtype != MemberTypes.NestedType)
271 throw new NotSupportedException (Locale.GetText (
272 "Element is not a constructor, method, property, event, type or field."));
274 return ((MemberInfo) element).IsDefined (attributeType, inherit);
277 public static bool IsDefined (Assembly element, Type attributeType, bool inherit)
279 CheckParameters (element, attributeType);
281 return element.IsDefined (attributeType, inherit);
284 public static bool IsDefined (Module element, Type attributeType, bool inherit)
286 CheckParameters (element, attributeType);
288 return element.IsDefined (attributeType, inherit);
291 public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit)
293 CheckParameters (element, attributeType);
295 return IsDefined (element.Member, attributeType, inherit);
298 public virtual bool Match (object obj)
300 // default action is the same as Equals.
301 // Derived classes should override as appropriate
302 return this.Equals (obj);
305 public override bool Equals (object obj)
307 if (obj == null || !(obj is Attribute))
310 return ((Attribute) obj) == this;