1 // System.MonoCustomAttrs.cs
2 // Hooks into the runtime to get custom attributes for reflection handles
5 // Paolo Molaro (lupus@ximian.com)
6 // Gonzalo Paniagua Javier (gonzalo@ximian.com)
8 // (c) 2002,2003 Ximian, Inc. (http://www.ximian.com)
12 // Copyright (C) 2004 Novell, Inc (http://www.novell.com)
14 // Permission is hereby granted, free of charge, to any person obtaining
15 // a copy of this software and associated documentation files (the
16 // "Software"), to deal in the Software without restriction, including
17 // without limitation the rights to use, copy, modify, merge, publish,
18 // distribute, sublicense, and/or sell copies of the Software, and to
19 // permit persons to whom the Software is furnished to do so, subject to
20 // the following conditions:
22 // The above copyright notice and this permission notice shall be
23 // included in all copies or substantial portions of the Software.
25 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
26 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
27 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
28 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
29 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
30 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
31 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
35 using System.Reflection;
36 using System.Collections;
37 using System.Runtime.CompilerServices;
39 using System.Reflection.Emit;
42 using System.Collections.Generic;
46 internal class MonoCustomAttrs
48 static Assembly corlib;
50 /* Treat as user types all corlib types extending System.Type that are not MonoType and TypeBuilder */
51 static bool IsUserCattrProvider (object obj)
53 Type type = obj as Type;
55 if ((type is MonoType) || (type is TypeBuilder))
63 corlib = typeof (int).Assembly;
64 return obj.GetType ().Assembly != corlib;
67 [MethodImplAttribute (MethodImplOptions.InternalCall)]
68 internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, Type attributeType, bool pseudoAttrs);
70 internal static object[] GetPseudoCustomAttributes (ICustomAttributeProvider obj, Type attributeType) {
71 object[] pseudoAttrs = null;
73 /* FIXME: Add other types */
74 if (obj is MonoMethod)
75 pseudoAttrs = ((MonoMethod)obj).GetPseudoCustomAttributes ();
76 else if (obj is FieldInfo)
77 pseudoAttrs = ((FieldInfo)obj).GetPseudoCustomAttributes ();
78 else if (obj is ParameterInfo)
79 pseudoAttrs = ((ParameterInfo)obj).GetPseudoCustomAttributes ();
81 pseudoAttrs = ((Type)obj).GetPseudoCustomAttributes ();
83 if ((attributeType != null) && (pseudoAttrs != null)) {
84 for (int i = 0; i < pseudoAttrs.Length; ++i)
85 if (attributeType.IsAssignableFrom (pseudoAttrs [i].GetType ()))
86 if (pseudoAttrs.Length == 1)
89 return new object [] { pseudoAttrs [i] };
90 return new object [0];
96 internal static object[] GetCustomAttributesBase (ICustomAttributeProvider obj, Type attributeType)
99 if (IsUserCattrProvider (obj))
100 attrs = obj.GetCustomAttributes (attributeType, true);
102 attrs = GetCustomAttributesInternal (obj, attributeType, false);
104 object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
105 if (pseudoAttrs != null) {
106 object[] res = new object [attrs.Length + pseudoAttrs.Length];
107 System.Array.Copy (attrs, res, attrs.Length);
108 System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
115 internal static Attribute GetCustomAttribute (ICustomAttributeProvider obj,
119 object[] res = GetCustomAttributes (obj, attributeType, inherit);
124 else if (res.Length > 1)
126 string msg = "'{0}' has more than one attribute of type '{1}";
127 msg = String.Format (msg, obj, attributeType);
128 throw new AmbiguousMatchException (msg);
131 return (Attribute) res[0];
134 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type attributeType, bool inherit)
137 throw new ArgumentNullException ("obj");
138 if (attributeType == null)
139 throw new ArgumentNullException ("attributeType");
141 if (attributeType == typeof (MonoCustomAttrs))
142 attributeType = null;
145 object[] res = GetCustomAttributesBase (obj, attributeType);
147 if (!inherit && res.Length == 1)
150 throw new CustomAttributeFormatException ("Invalid custom attribute format");
152 if (attributeType != null)
154 if (attributeType.IsAssignableFrom (res[0].GetType ()))
156 r = (object[]) Array.CreateInstance (attributeType, 1);
161 r = (object[]) Array.CreateInstance (attributeType, 0);
166 r = (object[]) Array.CreateInstance (res[0].GetType (), 1);
172 // if AttributeType is sealed, and Inherited is set to false, then
173 // there's no use in scanning base types
174 if ((attributeType != null && attributeType.IsSealed) && inherit)
176 AttributeUsageAttribute usageAttribute = RetrieveAttributeUsage (
178 if (!usageAttribute.Inherited)
184 int initialSize = res.Length < 16 ? res.Length : 16;
186 Hashtable attributeInfos = new Hashtable (initialSize);
187 ArrayList a = new ArrayList (initialSize);
188 ICustomAttributeProvider btype = obj;
190 int inheritanceLevel = 0;
194 foreach (object attr in res)
196 AttributeUsageAttribute usage;
198 throw new CustomAttributeFormatException ("Invalid custom attribute format");
200 Type attrType = attr.GetType ();
201 if (attributeType != null)
203 if (!attributeType.IsAssignableFrom (attrType))
209 AttributeInfo firstAttribute = (AttributeInfo) attributeInfos[attrType];
210 if (firstAttribute != null)
212 usage = firstAttribute.Usage;
216 usage = RetrieveAttributeUsage (attrType);
219 // only add attribute to the list of attributes if
220 // - we are on the first inheritance level, or the attribute can be inherited anyway
222 // - multiple attributes of the type are allowed
224 // - this is the first attribute we've discovered
226 // - the attribute is on same inheritance level than the first
227 // attribute that was discovered for this attribute type ))
228 if ((inheritanceLevel == 0 || usage.Inherited) && (usage.AllowMultiple ||
229 (firstAttribute == null || (firstAttribute != null
230 && firstAttribute.InheritanceLevel == inheritanceLevel))))
235 if (firstAttribute == null)
237 attributeInfos.Add (attrType, new AttributeInfo (usage, inheritanceLevel));
241 if ((btype = GetBase (btype)) != null)
244 res = GetCustomAttributesBase (btype, attributeType);
246 } while (inherit && btype != null);
248 object[] array = null;
249 if (attributeType == null || attributeType.IsValueType)
251 array = (object[]) Array.CreateInstance (typeof(Attribute), a.Count);
255 array = Array.CreateInstance (attributeType, a.Count) as object[];
258 // copy attributes to array
264 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, bool inherit)
267 throw new ArgumentNullException ("obj");
270 return (object[]) GetCustomAttributesBase (obj, null).Clone ();
272 return GetCustomAttributes (obj, typeof (MonoCustomAttrs), inherit);
275 [MethodImplAttribute (MethodImplOptions.InternalCall)]
276 static extern CustomAttributeData [] GetCustomAttributesDataInternal (ICustomAttributeProvider obj);
278 internal static IList<CustomAttributeData> GetCustomAttributesData (ICustomAttributeProvider obj)
281 throw new ArgumentNullException ("obj");
283 CustomAttributeData [] attrs = GetCustomAttributesDataInternal (obj);
284 return Array.AsReadOnly<CustomAttributeData> (attrs);
287 internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
289 if (attributeType == null)
290 throw new ArgumentNullException ("attributeType");
292 AttributeUsageAttribute usage = null;
294 if (IsUserCattrProvider (obj))
295 return obj.IsDefined (attributeType, inherit);
297 if (IsDefinedInternal (obj, attributeType))
300 object[] pseudoAttrs = GetPseudoCustomAttributes (obj, attributeType);
301 if (pseudoAttrs != null) {
302 for (int i = 0; i < pseudoAttrs.Length; ++i)
303 if (attributeType.IsAssignableFrom (pseudoAttrs[i].GetType ()))
311 usage = RetrieveAttributeUsage (attributeType);
312 if (!usage.Inherited)
317 } while (obj != null);
322 [MethodImplAttribute (MethodImplOptions.InternalCall)]
323 internal static extern bool IsDefinedInternal (ICustomAttributeProvider obj, Type AttributeType);
325 static PropertyInfo GetBasePropertyDefinition (PropertyInfo property)
327 MethodInfo method = property.GetGetMethod (true);
328 if (method == null || !method.IsVirtual)
329 method = property.GetSetMethod (true);
330 if (method == null || !method.IsVirtual)
333 MethodInfo baseMethod = method.GetBaseMethod ();
334 if (baseMethod != null && baseMethod != method) {
335 ParameterInfo[] parameters = property.GetIndexParameters ();
336 if (parameters != null && parameters.Length > 0) {
337 Type[] paramTypes = new Type[parameters.Length];
338 for (int i=0; i < paramTypes.Length; i++)
339 paramTypes[i] = parameters[i].ParameterType;
340 return baseMethod.DeclaringType.GetProperty (property.Name, property.PropertyType,
343 return baseMethod.DeclaringType.GetProperty (property.Name, property.PropertyType);
350 static EventInfo GetBaseEventDefinition (EventInfo evt)
352 MethodInfo method = evt.GetAddMethod (true);
353 if (method == null || !method.IsVirtual)
354 method = evt.GetRaiseMethod (true);
355 if (method == null || !method.IsVirtual)
356 method = evt.GetRemoveMethod (true);
357 if (method == null || !method.IsVirtual)
360 MethodInfo baseMethod = method.GetBaseMethod ();
361 if (baseMethod != null && baseMethod != method) {
362 BindingFlags flags = method.IsPublic ? BindingFlags.Public : BindingFlags.NonPublic;
363 flags |= method.IsStatic ? BindingFlags.Static : BindingFlags.Instance;
365 return baseMethod.DeclaringType.GetEvent (evt.Name, flags);
370 // Handles Type, MonoProperty and MonoMethod.
371 // The runtime has also cases for MonoEvent, MonoField, Assembly and ParameterInfo,
372 // but for those we return null here.
373 static ICustomAttributeProvider GetBase (ICustomAttributeProvider obj)
379 return ((Type) obj).BaseType;
381 MethodInfo method = null;
382 if (obj is MonoProperty)
383 return GetBasePropertyDefinition ((MonoProperty) obj);
384 else if (obj is MonoEvent)
385 return GetBaseEventDefinition ((MonoEvent)obj);
386 else if (obj is MonoMethod)
387 method = (MethodInfo) obj;
390 * ParameterInfo -> null
395 if (method == null || !method.IsVirtual)
398 MethodInfo baseMethod = method.GetBaseMethod ();
399 if (baseMethod == method)
405 private static AttributeUsageAttribute RetrieveAttributeUsage (Type attributeType)
407 if (attributeType == typeof (AttributeUsageAttribute))
408 /* Avoid endless recursion */
409 return new AttributeUsageAttribute (AttributeTargets.Class);
411 AttributeUsageAttribute usageAttribute = null;
412 object[] attribs = GetCustomAttributes (attributeType, typeof(AttributeUsageAttribute), false);
413 if (attribs.Length == 0)
415 // if no AttributeUsage was defined on the attribute level, then
416 // try to retrieve if from its base type
417 if (attributeType.BaseType != null)
419 usageAttribute = RetrieveAttributeUsage (attributeType.BaseType);
422 if (usageAttribute != null)
424 // return AttributeUsage of base class
425 return usageAttribute;
428 // return default AttributeUsageAttribute if no AttributeUsage
429 // was defined on attribute, or its base class
430 return DefaultAttributeUsage;
432 // check if more than one AttributeUsageAttribute has been specified
434 // NOTE: compilers should prevent this, but that doesn't prevent
435 // anyone from using IL ofcourse
436 if (attribs.Length > 1)
438 throw new FormatException ("Duplicate AttributeUsageAttribute cannot be specified on an attribute type.");
441 return ((AttributeUsageAttribute) attribs[0]);
444 private static readonly AttributeUsageAttribute DefaultAttributeUsage =
445 new AttributeUsageAttribute (AttributeTargets.All);
447 private class AttributeInfo
449 private AttributeUsageAttribute _usage;
450 private int _inheritanceLevel;
452 public AttributeInfo (AttributeUsageAttribute usage, int inheritanceLevel)
455 _inheritanceLevel = inheritanceLevel;
458 public AttributeUsageAttribute Usage
466 public int InheritanceLevel
470 return _inheritanceLevel;