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;
41 internal class MonoCustomAttrs
43 [MethodImplAttribute (MethodImplOptions.InternalCall)]
44 internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, bool pseudoAttrs);
46 internal static object[] GetCustomAttributesBase (ICustomAttributeProvider obj)
48 object[] attrs = GetCustomAttributesInternal (obj, false);
50 #if NET_2_0 || BOOTSTRAP_NET_2_0
51 object[] pseudoAttrs = null;
53 /* FIXME: Add other types */
55 pseudoAttrs = ((Type)obj).GetPseudoCustomAttributes ();
57 if (pseudoAttrs != null) {
58 object[] res = new object [attrs.Length + pseudoAttrs.Length];
59 System.Array.Copy (attrs, res, attrs.Length);
60 System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
70 internal static Attribute GetCustomAttribute (ICustomAttributeProvider obj,
74 object[] res = GetCustomAttributes (obj, attributeType, inherit);
79 else if (res.Length > 1)
81 string msg = "'{0}' has more than one attribute of type '{1}";
82 msg = String.Format (msg, obj, attributeType);
83 throw new AmbiguousMatchException (msg);
86 return (Attribute) res[0];
89 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type attributeType, bool inherit)
92 throw new ArgumentNullException ("obj");
95 object[] res = GetCustomAttributesBase (obj);
97 if (!inherit && res.Length == 1)
99 if (attributeType != null)
101 if (attributeType.IsAssignableFrom (res[0].GetType ()))
103 r = (object[]) Array.CreateInstance (attributeType, 1);
108 r = (object[]) Array.CreateInstance (attributeType, 0);
113 r = (object[]) Array.CreateInstance (res[0].GetType (), 1);
119 // if AttributeType is sealed, and Inherited is set to false, then
120 // there's no use in scanning base types
121 if ((attributeType != null && attributeType.IsSealed) && inherit)
123 AttributeUsageAttribute usageAttribute = RetrieveAttributeUsage (
125 if (!usageAttribute.Inherited)
131 int initialSize = res.Length < 16 ? res.Length : 16;
133 Hashtable attributeInfos = new Hashtable (initialSize);
134 ArrayList a = new ArrayList (initialSize);
135 ICustomAttributeProvider btype = obj;
137 int inheritanceLevel = 0;
141 foreach (object attr in res)
143 AttributeUsageAttribute usage;
145 Type attrType = attr.GetType ();
146 if (attributeType != null)
148 if (!attributeType.IsAssignableFrom (attrType))
154 AttributeInfo firstAttribute = (AttributeInfo) attributeInfos[attrType];
155 if (firstAttribute != null)
157 usage = firstAttribute.Usage;
161 usage = RetrieveAttributeUsage (attrType);
164 // only add attribute to the list of attributes if
165 // - we are on the first inheritance level, or the attribute can be inherited anyway
167 // - multiple attributes of the type are allowed
169 // - this is the first attribute we've discovered
171 // - the attribute is on same inheritance level than the first
172 // attribute that was discovered for this attribute type ))
173 if ((inheritanceLevel == 0 || usage.Inherited) && (usage.AllowMultiple ||
174 (firstAttribute == null || (firstAttribute != null
175 && firstAttribute.InheritanceLevel == inheritanceLevel))))
180 if (firstAttribute == null)
182 attributeInfos.Add (attrType, new AttributeInfo (usage, inheritanceLevel));
186 if ((btype = GetBase (btype)) != null)
189 res = GetCustomAttributesBase (btype);
191 } while (inherit && btype != null);
193 object[] array = null;
194 if (attributeType == null || attributeType.IsValueType)
196 array = (object[]) Array.CreateInstance (typeof(Attribute), a.Count);
200 array = Array.CreateInstance (attributeType, a.Count) as object[];
203 // copy attributes to array
209 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, bool inherit)
212 throw new ArgumentNullException ("obj");
215 return (object[]) GetCustomAttributesBase (obj).Clone ();
217 return GetCustomAttributes (obj, null, inherit);
220 internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
222 object [] res = GetCustomAttributesBase (obj);
223 foreach (object attr in res)
224 if (attributeType.Equals (attr.GetType ()))
227 ICustomAttributeProvider btype;
228 if (inherit && ((btype = GetBase (obj)) != null))
229 return IsDefined (btype, attributeType, inherit);
234 // Handles Type, MonoProperty and MonoMethod.
235 // The runtime has also cases for MonoEvent, MonoField, Assembly and ParameterInfo,
236 // but for those we return null here.
237 static ICustomAttributeProvider GetBase (ICustomAttributeProvider obj)
243 return ((Type) obj).BaseType;
245 MethodInfo method = null;
246 if (obj is MonoProperty)
248 MonoProperty prop = (MonoProperty) obj;
249 method = prop.GetGetMethod (true);
251 method = prop.GetSetMethod (true);
253 else if (obj is MonoMethod)
255 method = (MethodInfo) obj;
259 * ParameterInfo -> null
264 if (method == null || !method.IsVirtual)
267 MethodInfo baseMethod = method.GetBaseDefinition ();
268 if (baseMethod == method)
274 private static AttributeUsageAttribute RetrieveAttributeUsage (Type attributeType)
276 if (attributeType == typeof (AttributeUsageAttribute))
277 /* Avoid endless recursion */
278 return new AttributeUsageAttribute (AttributeTargets.Class);
280 AttributeUsageAttribute usageAttribute = null;
281 object[] attribs = GetCustomAttributes (attributeType,
282 MonoCustomAttrs.AttributeUsageType, false);
283 if (attribs.Length == 0)
285 // if no AttributeUsage was defined on the attribute level, then
286 // try to retrieve if from its base type
287 if (attributeType.BaseType != null)
289 usageAttribute = RetrieveAttributeUsage (attributeType.BaseType);
292 if (usageAttribute != null)
294 // return AttributeUsage of base class
295 return usageAttribute;
298 // return default AttributeUsageAttribute if no AttributeUsage
299 // was defined on attribute, or its base class
300 return DefaultAttributeUsage;
302 // check if more than one AttributeUsageAttribute has been specified
304 // NOTE: compilers should prevent this, but that doesn't prevent
305 // anyone from using IL ofcourse
306 if (attribs.Length > 1)
308 throw new FormatException ("Duplicate AttributeUsageAttribute cannot be specified on an attribute type.");
311 return ((AttributeUsageAttribute) attribs[0]);
314 private static readonly Type AttributeUsageType = typeof(AttributeUsageAttribute);
315 private static readonly AttributeUsageAttribute DefaultAttributeUsage =
316 new AttributeUsageAttribute (AttributeTargets.All);
318 private class AttributeInfo
320 private AttributeUsageAttribute _usage;
321 private int _inheritanceLevel;
323 public AttributeInfo (AttributeUsageAttribute usage, int inheritanceLevel)
326 _inheritanceLevel = inheritanceLevel;
329 public AttributeUsageAttribute Usage
337 public int InheritanceLevel
341 return _inheritanceLevel;