2005-01-31 Zoltan Varga <vargaz@freemail.hu>
[mono.git] / mcs / class / corlib / System / MonoCustomAttrs.cs
index c4ceb1d034610c89f94000941b0136c86f9d7506..4a00d26446edf57982764e80cbab879c37522208 100755 (executable)
@@ -41,7 +41,37 @@ namespace System
        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,
@@ -68,7 +98,7 @@ namespace System
                                throw new ArgumentNullException ("obj");
 
                        object[] r;
-                       object[] res = GetCustomAttributes (obj);
+                       object[] res = GetCustomAttributesBase (obj);
                        // shortcut
                        if (!inherit && res.Length == 1)
                        {
@@ -94,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);
@@ -162,7 +192,7 @@ namespace System
                                if ((btype = GetBase (btype)) != null)
                                {
                                        inheritanceLevel++;
-                                       res = GetCustomAttributes (btype);
+                                       res = GetCustomAttributesBase (btype);
                                }
                        } while (inherit && btype != null);
 
@@ -188,22 +218,20 @@ namespace System
                                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;
@@ -251,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);