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
13 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
15 // Permission is hereby granted, free of charge, to any person obtaining
16 // a copy of this software and associated documentation files (the
17 // "Software"), to deal in the Software without restriction, including
18 // without limitation the rights to use, copy, modify, merge, publish,
19 // distribute, sublicense, and/or sell copies of the Software, and to
20 // permit persons to whom the Software is furnished to do so, subject to
21 // the following conditions:
23 // The above copyright notice and this permission notice shall be
24 // included in all copies or substantial portions of the Software.
26 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
27 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
28 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
29 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
30 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
31 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
32 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
39 [AttributeUsage (AttributeTargets.All)]
41 public abstract class Attribute
43 protected Attribute ()
47 public virtual object TypeId {
49 // Derived classes should override this default behaviour as appropriate
50 return this.GetType ();
54 private static void CheckParameters (object element, Type attributeType)
56 // neither parameter is allowed to be null
58 throw new ArgumentNullException ("element");
60 if (attributeType == null)
61 throw new ArgumentNullException ("attributeType");
63 if (!typeof (Attribute).IsAssignableFrom (attributeType))
64 throw new ArgumentException (Locale.GetText (
65 "Type is not derived from System.Attribute."), "attributeType");
68 private static Attribute FindAttribute (object[] attributes)
70 // if there exists more than one attribute of the given type, throw an exception
71 if (attributes.Length > 1) {
72 throw new AmbiguousMatchException (Locale.GetText (
73 "<element> has more than one attribute of type <attribute_type>"));
76 if (attributes.Length < 1)
79 // tested above for '> 1' and and '< 1', so only '== 1' is left,
80 // i.e. we found the attribute
81 return (Attribute) attributes[0];
84 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType)
86 return GetCustomAttribute (element, attributeType, true);
89 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType)
91 return GetCustomAttribute (element, attributeType, true);
94 public static Attribute GetCustomAttribute (Assembly element, Type attributeType)
96 return GetCustomAttribute (element, attributeType, true);
99 public static Attribute GetCustomAttribute (Module element, Type attributeType)
101 return GetCustomAttribute (element, attributeType, true);
104 public static Attribute GetCustomAttribute (Module element, Type attributeType, bool inherit)
106 // neither parameter is allowed to be null
107 CheckParameters (element, attributeType);
109 // Module inheritance hierarchies CAN NOT be searched for attributes, so the second
110 // parameter of GetCustomAttributes () is IGNORED.
111 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
113 return FindAttribute (attributes);
116 public static Attribute GetCustomAttribute (Assembly element, Type attributeType, bool inherit)
118 // neither parameter is allowed to be null
119 CheckParameters (element, attributeType);
121 // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second
122 // parameter of GetCustomAttributes () is IGNORED.
123 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
125 return FindAttribute (attributes);
128 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType, bool inherit)
130 // neither parameter is allowed to be null
131 CheckParameters (element, attributeType);
133 // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second
134 // parameter of GetCustomAttributes () is IGNORED.
135 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
137 return FindAttribute (attributes);
140 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType, bool inherit)
142 // neither parameter is allowed to be null
143 CheckParameters (element, attributeType);
145 // MemberInfo inheritance hierarchies can be searched for attributes, so the second
146 // parameter of GetCustomAttribute () is respected.
147 return MonoCustomAttrs.GetCustomAttribute (element, attributeType, inherit);
150 public static Attribute[] GetCustomAttributes (Assembly element)
152 return GetCustomAttributes (element, true);
155 public static Attribute[] GetCustomAttributes (ParameterInfo element)
157 return GetCustomAttributes (element, true);
160 public static Attribute[] GetCustomAttributes (MemberInfo element)
162 return GetCustomAttributes (element, true);
165 public static Attribute[] GetCustomAttributes (Module element)
167 return GetCustomAttributes (element, true);
170 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType)
172 return GetCustomAttributes (element, attributeType, true);
175 public static Attribute[] GetCustomAttributes (Module element, Type attributeType)
177 return GetCustomAttributes (element, attributeType, true);
180 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType)
182 return GetCustomAttributes (element, attributeType, true);
185 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType)
187 return GetCustomAttributes (element, attributeType, true);
190 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit)
192 // element parameter is not allowed to be null
193 CheckParameters (element, attributeType);
195 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
198 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit)
200 // element parameter is not allowed to be null
201 CheckParameters (element, attributeType);
203 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
206 public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit)
208 // element parameter is not allowed to be null
209 CheckParameters (element, attributeType);
211 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
214 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit)
216 // element parameter is not allowed to be null
217 CheckParameters (element, attributeType);
219 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
222 public static Attribute[] GetCustomAttributes (Module element, bool inherit)
224 // element parameter is not allowed to be null
225 CheckParameters (element, typeof (Attribute));
227 return (Attribute []) element.GetCustomAttributes (inherit);
230 public static Attribute[] GetCustomAttributes (Assembly element, bool inherit)
232 // element parameter is not allowed to be null
233 CheckParameters (element, typeof (Attribute));
235 return (Attribute []) element.GetCustomAttributes (inherit);
238 public static Attribute[] GetCustomAttributes (MemberInfo element, bool inherit)
240 // element parameter is not allowed to be null
241 CheckParameters (element, typeof (Attribute));
243 return (Attribute []) element.GetCustomAttributes (inherit);
246 public static Attribute[] GetCustomAttributes (ParameterInfo element, bool inherit)
248 // element parameter is not allowed to be null
249 CheckParameters (element, typeof (Attribute));
251 return (Attribute []) element.GetCustomAttributes (inherit);
254 public override int GetHashCode ()
256 return base.GetHashCode ();
259 public virtual bool IsDefaultAttribute ()
261 // Derived classes should override this default behaviour as appropriate
265 public static bool IsDefined (Module element, Type attributeType)
267 return IsDefined (element, attributeType, false);
270 public static bool IsDefined (ParameterInfo element, Type attributeType)
272 return IsDefined (element, attributeType, true);
275 public static bool IsDefined (MemberInfo element, Type attributeType)
277 return IsDefined (element, attributeType, true);
280 public static bool IsDefined (Assembly element, Type attributeType)
282 return IsDefined (element, attributeType, true);
285 public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit)
287 CheckParameters (element, attributeType);
289 MemberTypes mtype = element.MemberType;
290 if (mtype != MemberTypes.Constructor && mtype != MemberTypes.Event &&
291 mtype != MemberTypes.Field && mtype != MemberTypes.Method &&
292 mtype != MemberTypes.Property && mtype != MemberTypes.TypeInfo &&
293 mtype != MemberTypes.NestedType)
294 throw new NotSupportedException (Locale.GetText (
295 "Element is not a constructor, method, property, event, type or field."));
297 return ((MemberInfo) element).IsDefined (attributeType, inherit);
300 public static bool IsDefined (Assembly element, Type attributeType, bool inherit)
302 CheckParameters (element, attributeType);
304 return element.IsDefined (attributeType, inherit);
307 public static bool IsDefined (Module element, Type attributeType, bool inherit)
309 CheckParameters (element, attributeType);
311 return element.IsDefined (attributeType, inherit);
314 public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit)
316 CheckParameters (element, attributeType);
318 return IsDefined (element.Member, attributeType, inherit);
321 public virtual bool Match (object obj)
323 // default action is the same as Equals.
324 // Derived classes should override as appropriate
325 return this.Equals (obj);
328 public override bool Equals (object obj)
330 if (obj == null || !(obj is Attribute))
333 return ((Attribute) obj) == this;