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)]
45 [ClassInterfaceAttribute (ClassInterfaceType.None)]
46 public abstract class Attribute : _Attribute {
48 public abstract class Attribute {
50 protected Attribute ()
54 public virtual object TypeId {
56 // Derived classes should override this default behaviour as appropriate
57 return this.GetType ();
61 private static void CheckParameters (object element, Type attributeType)
63 // neither parameter is allowed to be null
65 throw new ArgumentNullException ("element");
67 if (attributeType == null)
68 throw new ArgumentNullException ("attributeType");
70 if (!typeof (Attribute).IsAssignableFrom (attributeType))
71 throw new ArgumentException (Locale.GetText (
72 "Type is not derived from System.Attribute."), "attributeType");
75 private static Attribute FindAttribute (object[] attributes)
77 // if there exists more than one attribute of the given type, throw an exception
78 if (attributes.Length > 1) {
79 throw new AmbiguousMatchException (Locale.GetText (
80 "<element> has more than one attribute of type <attribute_type>"));
83 if (attributes.Length < 1)
86 // tested above for '> 1' and and '< 1', so only '== 1' is left,
87 // i.e. we found the attribute
88 return (Attribute) attributes[0];
91 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType)
93 return GetCustomAttribute (element, attributeType, true);
96 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType)
98 return GetCustomAttribute (element, attributeType, true);
101 public static Attribute GetCustomAttribute (Assembly element, Type attributeType)
103 return GetCustomAttribute (element, attributeType, true);
106 public static Attribute GetCustomAttribute (Module element, Type attributeType)
108 return GetCustomAttribute (element, attributeType, true);
111 public static Attribute GetCustomAttribute (Module element, Type attributeType, bool inherit)
113 // neither parameter is allowed to be null
114 CheckParameters (element, attributeType);
116 // Module inheritance hierarchies CAN NOT be searched for attributes, so the second
117 // parameter of GetCustomAttributes () is IGNORED.
118 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
120 return FindAttribute (attributes);
123 public static Attribute GetCustomAttribute (Assembly element, Type attributeType, bool inherit)
125 // neither parameter is allowed to be null
126 CheckParameters (element, attributeType);
128 // Assembly inheritance hierarchies CAN NOT be searched for attributes, so the second
129 // parameter of GetCustomAttributes () is IGNORED.
130 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
132 return FindAttribute (attributes);
135 public static Attribute GetCustomAttribute (ParameterInfo element, Type attributeType, bool inherit)
137 // neither parameter is allowed to be null
138 CheckParameters (element, attributeType);
140 // ParameterInfo inheritance hierarchies CAN NOT be searched for attributes, so the second
141 // parameter of GetCustomAttributes () is IGNORED.
142 object[] attributes = element.GetCustomAttributes (attributeType, inherit);
144 return FindAttribute (attributes);
147 public static Attribute GetCustomAttribute (MemberInfo element, Type attributeType, bool inherit)
149 // neither parameter is allowed to be null
150 CheckParameters (element, attributeType);
152 // MemberInfo inheritance hierarchies can be searched for attributes, so the second
153 // parameter of GetCustomAttribute () is respected.
154 return MonoCustomAttrs.GetCustomAttribute (element, attributeType, inherit);
157 public static Attribute[] GetCustomAttributes (Assembly element)
159 return GetCustomAttributes (element, true);
162 public static Attribute[] GetCustomAttributes (ParameterInfo element)
164 return GetCustomAttributes (element, true);
167 public static Attribute[] GetCustomAttributes (MemberInfo element)
169 return GetCustomAttributes (element, true);
172 public static Attribute[] GetCustomAttributes (Module element)
174 return GetCustomAttributes (element, true);
177 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType)
179 return GetCustomAttributes (element, attributeType, true);
182 public static Attribute[] GetCustomAttributes (Module element, Type attributeType)
184 return GetCustomAttributes (element, attributeType, true);
187 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType)
189 return GetCustomAttributes (element, attributeType, true);
192 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType)
194 return GetCustomAttributes (element, attributeType, true);
197 public static Attribute[] GetCustomAttributes (Assembly element, Type attributeType, bool inherit)
199 // element parameter is not allowed to be null
200 CheckParameters (element, attributeType);
202 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
205 public static Attribute[] GetCustomAttributes (ParameterInfo element, Type attributeType, bool inherit)
207 // element parameter is not allowed to be null
208 CheckParameters (element, attributeType);
210 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
213 public static Attribute[] GetCustomAttributes (Module element, Type attributeType, bool inherit)
215 // element parameter is not allowed to be null
216 CheckParameters (element, attributeType);
218 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
221 public static Attribute[] GetCustomAttributes (MemberInfo element, Type attributeType, bool inherit)
223 // element parameter is not allowed to be null
224 CheckParameters (element, attributeType);
226 return (Attribute []) element.GetCustomAttributes (attributeType, inherit);
229 public static Attribute[] GetCustomAttributes (Module element, bool inherit)
231 // element parameter is not allowed to be null
232 CheckParameters (element, typeof (Attribute));
234 return (Attribute []) element.GetCustomAttributes (inherit);
237 public static Attribute[] GetCustomAttributes (Assembly 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 (MemberInfo 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 (ParameterInfo element, bool inherit)
255 // element parameter is not allowed to be null
256 CheckParameters (element, typeof (Attribute));
258 return (Attribute []) element.GetCustomAttributes (inherit);
261 public override int GetHashCode ()
263 return base.GetHashCode ();
266 public virtual bool IsDefaultAttribute ()
268 // Derived classes should override this default behaviour as appropriate
272 public static bool IsDefined (Module element, Type attributeType)
274 return IsDefined (element, attributeType, false);
277 public static bool IsDefined (ParameterInfo element, Type attributeType)
279 return IsDefined (element, attributeType, true);
282 public static bool IsDefined (MemberInfo element, Type attributeType)
284 return IsDefined (element, attributeType, true);
287 public static bool IsDefined (Assembly element, Type attributeType)
289 return IsDefined (element, attributeType, true);
292 public static bool IsDefined (MemberInfo element, Type attributeType, bool inherit)
294 CheckParameters (element, attributeType);
296 MemberTypes mtype = element.MemberType;
297 if (mtype != MemberTypes.Constructor && mtype != MemberTypes.Event &&
298 mtype != MemberTypes.Field && mtype != MemberTypes.Method &&
299 mtype != MemberTypes.Property && mtype != MemberTypes.TypeInfo &&
300 mtype != MemberTypes.NestedType)
301 throw new NotSupportedException (Locale.GetText (
302 "Element is not a constructor, method, property, event, type or field."));
304 return ((MemberInfo) element).IsDefined (attributeType, inherit);
307 public static bool IsDefined (Assembly element, Type attributeType, bool inherit)
309 CheckParameters (element, attributeType);
311 return element.IsDefined (attributeType, inherit);
314 public static bool IsDefined (Module element, Type attributeType, bool inherit)
316 CheckParameters (element, attributeType);
318 return element.IsDefined (attributeType, inherit);
322 public static bool IsDefined (ParameterInfo element, Type attributeType, bool inherit)
324 CheckParameters (element, attributeType);
326 if (element.IsDefined (attributeType, inherit))
329 // FIXME: MS walks up the inheritance chain in some crazy way
330 return IsDefined (element.Member, attributeType, inherit);
333 public virtual bool Match (object obj)
335 // default action is the same as Equals.
336 // Derived classes should override as appropriate
337 return this.Equals (obj);
340 public override bool Equals (object obj)
342 if (obj == null || !(obj is Attribute))
346 // This is needed because Attribute.Equals does a deep
347 // compare. Ran into this with vbnc
349 return ValueType.DefaultEquals (this, obj);
353 void _Attribute.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
355 throw new NotImplementedException ();
358 void _Attribute.GetTypeInfo (uint iTInfo, uint lcid, IntPtr ppTInfo)
360 throw new NotImplementedException ();
363 void _Attribute.GetTypeInfoCount (out uint pcTInfo)
365 throw new NotImplementedException ();
368 void _Attribute.Invoke (uint dispIdMember, [In] ref Guid riid, uint lcid, short wFlags, IntPtr pDispParams,
369 IntPtr pVarResult, IntPtr pExcepInfo, IntPtr puArgErr)
371 throw new NotImplementedException ();