2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / MonoCustomAttrs.cs
index e223ec88db251b008f1967445f47652d4c1dbad0..4a00d26446edf57982764e80cbab879c37522208 100755 (executable)
@@ -40,14 +40,38 @@ namespace System
 {
        internal class MonoCustomAttrs
        {
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               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
-               internal static readonly bool pseudoAttrs = true;
+                       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
-               internal static readonly bool pseudoAttrs = false;
+                       return attrs;
 #endif
-
-               [MethodImplAttribute (MethodImplOptions.InternalCall)]
-               internal static extern object[] GetCustomAttributesInternal (ICustomAttributeProvider obj, bool pseudoAttrs);
+               }
 
                internal static Attribute GetCustomAttribute (ICustomAttributeProvider obj,
                                                                Type attributeType,
@@ -74,7 +98,7 @@ namespace System
                                throw new ArgumentNullException ("obj");
 
                        object[] r;
-                       object[] res = GetCustomAttributesInternal (obj, pseudoAttrs);
+                       object[] res = GetCustomAttributesBase (obj);
                        // shortcut
                        if (!inherit && res.Length == 1)
                        {
@@ -100,7 +124,7 @@ namespace System
 
                        // 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);
@@ -168,7 +192,7 @@ namespace System
                                if ((btype = GetBase (btype)) != null)
                                {
                                        inheritanceLevel++;
-                                       res = GetCustomAttributesInternal (btype, pseudoAttrs);
+                                       res = GetCustomAttributesBase (btype);
                                }
                        } while (inherit && btype != null);
 
@@ -194,14 +218,14 @@ namespace System
                                throw new ArgumentNullException ("obj");
 
                        if (!inherit)
-                               return (object[]) GetCustomAttributesInternal (obj, pseudoAttrs).Clone ();
+                               return (object[]) GetCustomAttributesBase (obj).Clone ();
 
                        return GetCustomAttributes (obj, null, inherit);
                }
 
                internal static bool IsDefined (ICustomAttributeProvider obj, Type attributeType, bool inherit)
                {
-                       object [] res = GetCustomAttributesInternal (obj, pseudoAttrs);
+                       object [] res = GetCustomAttributesBase (obj);
                        foreach (object attr in res)
                                if (attributeType.Equals (attr.GetType ()))
                                        return true;
@@ -255,6 +279,10 @@ namespace System
 
                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);