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
10 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
12 // Permission is hereby granted, free of charge, to any person obtaining
13 // a copy of this software and associated documentation files (the
14 // "Software"), to deal in the Software without restriction, including
15 // without limitation the rights to use, copy, modify, merge, publish,
16 // distribute, sublicense, and/or sell copies of the Software, and to
17 // permit persons to whom the Software is furnished to do so, subject to
18 // the following conditions:
20 // The above copyright notice and this permission notice shall be
21 // included in all copies or substantial portions of the Software.
23 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
24 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
25 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
26 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
27 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
28 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
29 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.Reflection;
33 using System.Runtime.InteropServices;
37 [AttributeUsage (AttributeTargets.All)]
42 [ComDefaultInterface (typeof (_Attribute))]
46 [ClassInterfaceAttribute (ClassInterfaceType.None)]
47 public abstract class Attribute : _Attribute {
49 public abstract class Attribute {
51 protected Attribute ()
55 public virtual object TypeId {
57 // Derived classes should override this default behaviour as appropriate
58 return this.GetType ();
62 private static void CheckParameters (object element, Type attributeType)
64 // neither parameter is allowed to be null
66 throw new ArgumentNullException ("element");
68 if (attributeType == null)
69 throw new ArgumentNullException ("attributeType");
71 if (!typeof (Attribute).IsAssignableFrom (attributeType))
72 throw new ArgumentException (Locale.GetText (
73 "Type is not derived from System.Attribute."), "attributeType");
76 private static Attribute FindAttribute (object[] attributes)
78 // if there exists more than one attribute of the given type, throw an exception
79 if (attributes.Length > 1) {
80 throw new AmbiguousMatchException (Locale.GetText (
81 "<element> has more than one attribute of type <attribute_type>"));
84 if (attributes.Length < 1)
87 // tested above for '> 1' and and '< 1', so only '== 1' is left,
88 // i.e. we found the attribute
89 return (Attribute) attributes[0];
92 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType)
94 return GetCustomAttribute (element, attributeType, true);
97 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType)
99 return GetCustomAttribute (element, attributeType, true);
102 public static Attribute GetCustomAttribute (Assembly element, Type attributeType)
104 return GetCustomAttribute (element, attributeType, true);
107 public static Attribute GetCustomAttribute (Module element, Type attributeType)
109 return GetCustomAttribute (element, attributeType, true);
112 public static Attribute GetCustomAttribute (Module element, Type attributeType, bool inherit)
114 // neither parameter is allowed to be null
115 CheckParameters (element, attributeType);
117 // Module inheritance hierarchies CAN NOT be searched for attributes, so the second
118 // parameter of GetCustomAttributes () is IGNORED.
119 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
121 return FindAttribute (attributes);
124 public static Attribute GetCustomAttribute (Assembly element, Type attributeType, bool inherit)
126 // neither parameter is allowed to be null
127 CheckParameters (element, attributeType);
129 // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second
130 // parameter of GetCustomAttributes () is IGNORED.
131 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
133 return FindAttribute (attributes);
136 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType, bool inherit)
138 // neither parameter is allowed to be null
139 CheckParameters (element, attributeType);
141 // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second
142 // parameter of GetCustomAttributes () is IGNORED.
143 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
145 return FindAttribute (attributes);
148 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType, bool inherit)
150 // neither parameter is allowed to be null
151 CheckParameters (element, attributeType);
153 // MemberInfo inheritance hierarchies can be searched for attributes, so the second
154 // parameter of GetCustomAttribute () is respected.
155 return MonoCustomAttrs.GetCustomAttribute (element, attributeType, inherit);
158 public static Attribute[] GetCustomAttributes (Assembly element)
160 return GetCustomAttributes (element, true);
163 public static Attribute[] GetCustomAttributes (ParameterInfo element)
165 return GetCustomAttributes (element, true);
168 public static Attribute[] GetCustomAttributes (MemberInfo element)
170 return GetCustomAttributes (element, true);
173 public static Attribute[] GetCustomAttributes (Module element)
175 return GetCustomAttributes (element, true);
178 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType)
180 return GetCustomAttributes (element, attributeType, true);
183 public static Attribute[] GetCustomAttributes (Module element, Type attributeType)
185 return GetCustomAttributes (element, attributeType, true);
188 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType)
190 return GetCustomAttributes (element, attributeType, true);
193 public static Attribute[] GetCustomAttributes (MemberInfo element, Type type)
195 return GetCustomAttributes (element, type, true);
198 public static Attribute[] GetCustomAttributes (Assembly 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 (ParameterInfo 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 (Module 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 (MemberInfo element, Type type, bool inherit)
224 // element parameter is not allowed to be null
225 CheckParameters (element, type);
227 // MS ignores the inherit param in PropertyInfo's ICustomAttributeProvider
228 // implementation, but not in the Attributes, so directly get the attributes
229 // from MonoCustomAttrs instead of going throught the PropertyInfo's
230 // ICustomAttributeProvider
231 MemberTypes mtype = element.MemberType;
232 if (mtype == MemberTypes.Property)
233 return (Attribute []) MonoCustomAttrs.GetCustomAttributes (element, type, inherit);
234 return (Attribute []) element.GetCustomAttributes (type, inherit);
237 public static Attribute[] GetCustomAttributes (Module element, bool inherit)
239 // element parameter is not allowed to be null
240 CheckParameters (element, typeof (Attribute));
242 return (Attribute []) element.GetCustomAttributes (inherit);
245 public static Attribute[] GetCustomAttributes (Assembly element, bool inherit)
247 // element parameter is not allowed to be null
248 CheckParameters (element, typeof (Attribute));
250 return (Attribute []) element.GetCustomAttributes (inherit);
253 public static Attribute[] GetCustomAttributes (MemberInfo element, bool inherit)
255 // element parameter is not allowed to be null
256 CheckParameters (element, typeof (Attribute));
258 // MS ignores the inherit param in PropertyInfo's ICustomAttributeProvider
259 // implementation, but not in the Attributes, so directly get the attributes
260 // from MonoCustomAttrs instead of going throught the PropertyInfo's
261 // ICustomAttributeProvider
262 MemberTypes mtype = element.MemberType;
263 if (mtype == MemberTypes.Property)
264 return (Attribute []) MonoCustomAttrs.GetCustomAttributes (element, inherit);
265 return (Attribute []) element.GetCustomAttributes (inherit);
268 public static Attribute[] GetCustomAttributes (ParameterInfo element, bool inherit)
270 // element parameter is not allowed to be null
271 CheckParameters (element, typeof (Attribute));
273 return (Attribute []) element.GetCustomAttributes (inherit);
276 public override int GetHashCode ()
278 return base.GetHashCode ();
281 public virtual bool IsDefaultAttribute ()
283 // Derived classes should override this default behaviour as appropriate
287 public static bool IsDefined (Module element, Type attributeType)
289 return IsDefined (element, attributeType, false);
292 public static bool IsDefined (ParameterInfo element, Type attributeType)
294 return IsDefined (element, attributeType, true);
297 public static bool IsDefined (MemberInfo element, Type attributeType)
299 return IsDefined (element, attributeType, true);
302 public static bool IsDefined (Assembly element, Type attributeType)
304 return IsDefined (element, attributeType, true);
307 public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit)
309 CheckParameters (element, attributeType);
311 MemberTypes mtype = element.MemberType;
312 if (mtype != MemberTypes.Constructor && mtype != MemberTypes.Event &&
313 mtype != MemberTypes.Field && mtype != MemberTypes.Method &&
314 mtype != MemberTypes.Property && mtype != MemberTypes.TypeInfo &&
315 mtype != MemberTypes.NestedType)
316 throw new NotSupportedException (Locale.GetText (
317 "Element is not a constructor, method, property, event, type or field."));
319 // MS ignores the inherit param in PropertyInfo's ICustomAttributeProvider
320 // implementation, but not in the Attributes, so directly get the attributes
321 // from MonoCustomAttrs instead of going throught the PropertyInfo's
322 // ICustomAttributeProvider
323 if (mtype == MemberTypes.Property)
324 return MonoCustomAttrs.IsDefined (element, attributeType, inherit);
326 return ((MemberInfo) element).IsDefined (attributeType, inherit);
329 public static bool IsDefined (Assembly element, Type attributeType, bool inherit)
331 CheckParameters (element, attributeType);
333 return element.IsDefined (attributeType, inherit);
336 public static bool IsDefined (Module element, Type attributeType, bool inherit)
338 CheckParameters (element, attributeType);
340 return element.IsDefined (attributeType, inherit);
343 // FIXME: MS apparently walks the inheritance way in some form.
344 public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit)
346 CheckParameters (element, attributeType);
348 if (element.IsDefined (attributeType, inherit))
351 // FIXME: MS walks up the inheritance chain in some crazy way
352 return IsDefined (element.Member, attributeType, inherit);
355 public virtual bool Match (object obj)
357 // default action is the same as Equals.
358 // Derived classes should override as appropriate
359 return this.Equals (obj);
362 public override bool Equals (object obj)
364 if (obj == null || !(obj is Attribute))
368 // This is needed because Attribute.Equals does a deep
369 // compare. Ran into this with vbnc
371 return ValueType.DefaultEquals (this, obj);
375 void _Attribute.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
377 throw new NotImplementedException ();
380 void _Attribute.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
382 throw new NotImplementedException ();
385 void _Attribute.GetTypeInfoCount (out uint pcTInfo)
387 throw new NotImplementedException ();
390 void _Attribute.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
391 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
393 throw new NotImplementedException ();