Add mono specific handling to attribute
authorMarek Safar <marek.safar@gmail.com>
Mon, 16 Mar 2015 10:01:01 +0000 (11:01 +0100)
committerMarek Safar <marek.safar@gmail.com>
Mon, 2 May 2016 22:08:02 +0000 (00:08 +0200)
mcs/class/referencesource/mscorlib/system/attribute.cs

index 76293f5a8abd4848d17215144d7b0d977b42839a..1fe2914ba58d2942e24509a0f9c70d0cf755377f 100644 (file)
@@ -23,7 +23,98 @@ namespace System {
     public abstract partial class Attribute : _Attribute
     {
         #region Private Statics
-#if !MONO
+#if MONO
+        static Attribute[] InternalGetCustomAttributes (PropertyInfo element, Type type, bool inherit)
+        {
+            return (Attribute []) MonoCustomAttrs.GetCustomAttributes (element, type, inherit);
+        }
+
+        static Attribute[] InternalGetCustomAttributes (EventInfo element, Type type, bool inherit)
+        {
+            return (Attribute []) MonoCustomAttrs.GetCustomAttributes (element, type, inherit);
+        }
+
+        static Attribute[] InternalParamGetCustomAttributes (ParameterInfo parameter, Type attributeType, bool inherit)
+        {
+            if (parameter.Member.MemberType != MemberTypes.Method)
+                return null;
+
+            var method = (MethodInfo) parameter.Member;
+            var definition = method.GetBaseDefinition ();
+
+            if (method == definition)
+                return null;
+
+            var types = new List<Type> ();
+            var custom_attributes = new List<Attribute> ();
+
+            if (attributeType == null)
+                attributeType = typeof (Attribute);
+
+            while (true) {
+                var param = method.GetParametersInternal () [parameter.Position];
+                var param_attributes = (Attribute []) param.GetCustomAttributes (attributeType, false);
+                foreach (var param_attribute in param_attributes) {
+                    var param_type = param_attribute.GetType ();
+                    if (types.Contains (param_type))
+                        continue;
+
+                    types.Add (param_type);
+                    custom_attributes.Add (param_attribute);
+                }
+
+                var base_method = method.GetBaseMethod ();
+                if (base_method == method)
+                    break;
+
+                method = base_method;
+            }
+
+            var attributes = (Attribute []) Array.CreateInstance (attributeType, custom_attributes.Count);
+            custom_attributes.CopyTo (attributes, 0);
+
+            return attributes;
+        }
+
+        static bool InternalIsDefined (PropertyInfo element, Type attributeType, bool inherit)
+        {
+            return MonoCustomAttrs.IsDefined (element, attributeType, inherit);
+        }
+
+        static bool InternalIsDefined (EventInfo element, Type attributeType, bool inherit)
+        {
+            return MonoCustomAttrs.IsDefined (element, attributeType, inherit);
+        }
+
+        static bool InternalParamIsDefined (ParameterInfo parameter, Type attributeType, bool inherit)
+        {
+            if (parameter.IsDefined (attributeType, inherit))
+                return true;
+
+            if (!inherit)
+                return false;
+
+            var member = parameter.Member;
+            if (member.MemberType != MemberTypes.Method)
+                return false;
+
+            var method = ((MethodInfo) member).GetBaseMethod ();
+
+            while (true) {
+                var param = method.GetParametersInternal () [parameter.Position];
+                if (param.IsDefined (attributeType, false))
+                    return true;
+
+                var base_method = method.GetBaseMethod ();
+                if (base_method == method)
+                    break;
+
+                method = base_method;
+            }
+
+            return false;
+        }
+#else
         #region PropertyInfo
         private static Attribute[] InternalGetCustomAttributes(PropertyInfo element, Type type, bool inherit)
         {