internal class MonoCustomAttrs
{
[MethodImplAttribute (MethodImplOptions.InternalCall)]
- internal static extern object[] GetCustomAttributes (ICustomAttributeProvider obj);
+ internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, bool pseudoAttrs);
+
+ internal static object[] GetCustomAttributesBase (ICustomAttributeProvider obj)
+ {
+ object[] attrs = GetCustomAttributesInternal (obj, false);
+
+#if NET_2_0 || BOOTSTRAP_NET_2_0
+ object[] pseudoAttrs = null;
+
+ /* FIXME: Add other types */
+ if (obj is Type)
+ pseudoAttrs = ((Type)obj).GetPseudoCustomAttributes ();
+ else if (obj is MonoMethod)
+ pseudoAttrs = ((MonoMethod)obj).GetPseudoCustomAttributes ();
+ else if (obj is FieldInfo)
+ pseudoAttrs = ((FieldInfo)obj).GetPseudoCustomAttributes ();
+ else if (obj is ParameterInfo)
+ pseudoAttrs = ((ParameterInfo)obj).GetPseudoCustomAttributes ();
+
+ if (pseudoAttrs != null) {
+ object[] res = new object [attrs.Length + pseudoAttrs.Length];
+ System.Array.Copy (attrs, res, attrs.Length);
+ System.Array.Copy (pseudoAttrs, 0, res, attrs.Length, pseudoAttrs.Length);
+ return res;
+ }
+ else
+ return attrs;
+#else
+ return attrs;
+#endif
+ }
internal static Attribute GetCustomAttribute (ICustomAttributeProvider obj,
Type attributeType,
throw new ArgumentNullException ("obj");
object[] r;
- object[] res = GetCustomAttributes (obj);
+ object[] res = GetCustomAttributesBase (obj);
// shortcut
if (!inherit && res.Length == 1)
{
// if AttributeType is sealed, and Inherited is set to false, then
// there's no use in scanning base types
- if ((attributeType != null && attributeType.IsSealed) && !inherit)
+ if ((attributeType != null && attributeType.IsSealed) && inherit)
{
AttributeUsageAttribute usageAttribute = RetrieveAttributeUsage (
attributeType);
if ((btype = GetBase (btype)) != null)
{
inheritanceLevel++;
- res = GetCustomAttributes (btype);
+ res = GetCustomAttributesBase (btype);
}
} while (inherit && btype != null);
throw new ArgumentNullException ("obj");
if (!inherit)
- return (object[]) GetCustomAttributes (obj).Clone ();
+ return (object[]) GetCustomAttributesBase (obj).Clone ();
return GetCustomAttributes (obj, null, inherit);
}
internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
{
- object[] res = GetCustomAttributes (obj);
+ object [] res = GetCustomAttributesBase (obj);
foreach (object attr in res)
- {
if (attributeType.Equals (attr.GetType ()))
return true;
- }
- ICustomAttributeProvider btype = GetBase (obj);
- if (inherit && (btype != null))
+ ICustomAttributeProvider btype;
+ if (inherit && ((btype = GetBase (obj)) != null))
return IsDefined (btype, attributeType, inherit);
return false;
private static AttributeUsageAttribute RetrieveAttributeUsage (Type attributeType)
{
+ if (attributeType == typeof (AttributeUsageAttribute))
+ /* Avoid endless recursion */
+ return new AttributeUsageAttribute (AttributeTargets.Class);
+
AttributeUsageAttribute usageAttribute = null;
object[] attribs = GetCustomAttributes (attributeType,
MonoCustomAttrs.AttributeUsageType, false);