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 ();
56 else if (obj is MonoMethod)
57 pseudoAttrs = ((MonoMethod)obj).GetPseudoCustomAttributes ();
58 else if (obj is FieldInfo)
59 pseudoAttrs = ((FieldInfo)obj).GetPseudoCustomAttributes ();
61 if (pseudoAttrs != null) {
62 object[] res = new object [attrs.Length + pseudoAttrs.Length];
63 System.Array.Copy (attrs, res, attrs.Length);
64 System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
74 internal static Attribute GetCustomAttribute (ICustomAttributeProvider obj,
78 object[] res = GetCustomAttributes (obj, attributeType, inherit);
83 else if (res.Length > 1)
85 string msg = "'{0}' has more than one attribute of type '{1}";
86 msg = String.Format (msg, obj, attributeType);
87 throw new AmbiguousMatchException (msg);
90 return (Attribute) res[0];
93 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, Type attributeType, bool inherit)
96 throw new ArgumentNullException ("obj");
99 object[] res = GetCustomAttributesBase (obj);
101 if (!inherit && res.Length == 1)
103 if (attributeType != null)
105 if (attributeType.IsAssignableFrom (res[0].GetType ()))
107 r = (object[]) Array.CreateInstance (attributeType, 1);
112 r = (object[]) Array.CreateInstance (attributeType, 0);
117 r = (object[]) Array.CreateInstance (res[0].GetType (), 1);
123 // if AttributeType is sealed, and Inherited is set to false, then
124 // there's no use in scanning base types
125 if ((attributeType != null && attributeType.IsSealed) && inherit)
127 AttributeUsageAttribute usageAttribute = RetrieveAttributeUsage (
129 if (!usageAttribute.Inherited)
135 int initialSize = res.Length < 16 ? res.Length : 16;
137 Hashtable attributeInfos = new Hashtable (initialSize);
138 ArrayList a = new ArrayList (initialSize);
139 ICustomAttributeProvider btype = obj;
141 int inheritanceLevel = 0;
145 foreach (object attr in res)
147 AttributeUsageAttribute usage;
149 Type attrType = attr.GetType ();
150 if (attributeType != null)
152 if (!attributeType.IsAssignableFrom (attrType))
158 AttributeInfo firstAttribute = (AttributeInfo) attributeInfos[attrType];
159 if (firstAttribute != null)
161 usage = firstAttribute.Usage;
165 usage = RetrieveAttributeUsage (attrType);
168 // only add attribute to the list of attributes if
169 // - we are on the first inheritance level, or the attribute can be inherited anyway
171 // - multiple attributes of the type are allowed
173 // - this is the first attribute we've discovered
175 // - the attribute is on same inheritance level than the first
176 // attribute that was discovered for this attribute type ))
177 if ((inheritanceLevel == 0 || usage.Inherited) && (usage.AllowMultiple ||
178 (firstAttribute == null || (firstAttribute != null
179 && firstAttribute.InheritanceLevel == inheritanceLevel))))
184 if (firstAttribute == null)
186 attributeInfos.Add (attrType, new AttributeInfo (usage, inheritanceLevel));
190 if ((btype = GetBase (btype)) != null)
193 res = GetCustomAttributesBase (btype);
195 } while (inherit && btype != null);
197 object[] array = null;
198 if (attributeType == null || attributeType.IsValueType)
200 array = (object[]) Array.CreateInstance (typeof(Attribute), a.Count);
204 array = Array.CreateInstance (attributeType, a.Count) as object[];
207 // copy attributes to array
213 internal static object[] GetCustomAttributes (ICustomAttributeProvider obj, bool inherit)
216 throw new ArgumentNullException ("obj");
219 return (object[]) GetCustomAttributesBase (obj).Clone ();
221 return GetCustomAttributes (obj, null, inherit);
224 internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
226 object [] res = GetCustomAttributesBase (obj);
227 foreach (object attr in res)
228 if (attributeType.Equals (attr.GetType ()))
231 ICustomAttributeProvider btype;
232 if (inherit && ((btype = GetBase (obj)) != null))
233 return IsDefined (btype, attributeType, inherit);
238 // Handles Type, MonoProperty and MonoMethod.
239 // The runtime has also cases for MonoEvent, MonoField, Assembly and ParameterInfo,
240 // but for those we return null here.
241 static ICustomAttributeProvider GetBase (ICustomAttributeProvider obj)
247 return ((Type) obj).BaseType;
249 MethodInfo method = null;
250 if (obj is MonoProperty)
252 MonoProperty prop = (MonoProperty) obj;
253 method = prop.GetGetMethod (true);
255 method = prop.GetSetMethod (true);
257 else if (obj is MonoMethod)
259 method = (MethodInfo) obj;
263 * ParameterInfo -> null
268 if (method == null || !method.IsVirtual)
271 MethodInfo baseMethod = method.GetBaseDefinition ();
272 if (baseMethod == method)
278 private static AttributeUsageAttribute RetrieveAttributeUsage (Type attributeType)
280 if (attributeType == typeof (AttributeUsageAttribute))
281 /* Avoid endless recursion */
282 return new AttributeUsageAttribute (AttributeTargets.Class);
284 AttributeUsageAttribute usageAttribute = null;
285 object[] attribs = GetCustomAttributes (attributeType,
286 MonoCustomAttrs.AttributeUsageType, false);
287 if (attribs.Length == 0)
289 // if no AttributeUsage was defined on the attribute level, then
290 // try to retrieve if from its base type
291 if (attributeType.BaseType != null)
293 usageAttribute = RetrieveAttributeUsage (attributeType.BaseType);
296 if (usageAttribute != null)
298 // return AttributeUsage of base class
299 return usageAttribute;
302 // return default AttributeUsageAttribute if no AttributeUsage
303 // was defined on attribute, or its base class
304 return DefaultAttributeUsage;
306 // check if more than one AttributeUsageAttribute has been specified
308 // NOTE: compilers should prevent this, but that doesn't prevent
309 // anyone from using IL ofcourse
310 if (attribs.Length > 1)
312 throw new FormatException ("Duplicate AttributeUsageAttribute cannot be specified on an attribute type.");
315 return ((AttributeUsageAttribute) attribs[0]);
318 private static readonly Type AttributeUsageType = typeof(AttributeUsageAttribute);
319 private static readonly AttributeUsageAttribute DefaultAttributeUsage =
320 new AttributeUsageAttribute (AttributeTargets.All);
322 private class AttributeInfo
324 private AttributeUsageAttribute _usage;
325 private int _inheritanceLevel;
327 public AttributeInfo (AttributeUsageAttribute usage, int inheritanceLevel)
330 _inheritanceLevel = inheritanceLevel;
333 public AttributeUsageAttribute Usage
341 public int InheritanceLevel
345 return _inheritanceLevel;