Merge pull request #819 from brendanzagaeski/patch-1
[mono.git] / mcs / class / corlib / System.Reflection / ParameterInfo.cs
index 2e8350c1a9cfaaf19aad32babbb348f0947eff0c..1c00fb96c10c03d9dd71430504032ae5f5f9a075 100644 (file)
@@ -1,9 +1,12 @@
 // System.Reflection.ParameterInfo
 //
-// Sean MacIsaac (macisaac@ximian.com)
+// Authors:
+//   Sean MacIsaac (macisaac@ximian.com)
+//   Marek Safar (marek.safar@gmail.com)
 //
 // (C) 2001 Ximian, Inc.
 // Copyright (C) 2004-2005 Novell, Inc (http://www.novell.com)
+// Copyright 2013 Xamarin, Inc (http://www.xamarin.com)
 //
 // Permission is hereby granted, free of charge, to any person obtaining
 // a copy of this software and associated documentation files (the
 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 //
 
+#if !FULL_AOT_RUNTIME
 using System.Reflection.Emit;
+#endif
 using System.Runtime.CompilerServices;
 using System.Runtime.InteropServices;
+using System.Collections.Generic;
+using System.Text;
 
 namespace System.Reflection
 {
-#if NET_2_0
        [ComVisible (true)]
        [ComDefaultInterfaceAttribute (typeof (_ParameterInfo))]
-#endif
        [Serializable]
        [ClassInterfaceAttribute (ClassInterfaceType.None)]
-       public class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
+       [StructLayout (LayoutKind.Sequential)]
+#if MOBILE
+       public partial class ParameterInfo : ICustomAttributeProvider {
+#else
+       public partial class ParameterInfo : ICustomAttributeProvider, _ParameterInfo {
+#endif
 
                protected Type ClassImpl;
                protected object DefaultValueImpl;
@@ -45,35 +55,11 @@ namespace System.Reflection
                protected string NameImpl;
                protected int PositionImpl;
                protected ParameterAttributes AttrsImpl;
-               private UnmanagedMarshal marshalAs;
+               internal MarshalAsAttribute marshalAs;
 
                protected ParameterInfo () {
                }
 
-               internal ParameterInfo (ParameterBuilder pb, Type type, MemberInfo member, int position) {
-                       this.ClassImpl = type;
-                       this.MemberImpl = member;
-                       if (pb != null) {
-                               this.NameImpl = pb.Name;
-                               this.PositionImpl = pb.Position - 1;    // ParameterInfo.Position is zero-based
-                               this.AttrsImpl = (ParameterAttributes) pb.Attributes;
-                       } else {
-                               this.NameImpl = "";
-                               this.PositionImpl = position - 1;
-                               this.AttrsImpl = ParameterAttributes.None;
-                       }
-               }
-
-               /* to build a ParameterInfo for the return type of a method */
-               internal ParameterInfo (Type type, MemberInfo member, UnmanagedMarshal marshalAs) {
-                       this.ClassImpl = type;
-                       this.MemberImpl = member;
-                       this.NameImpl = "";
-                       this.PositionImpl = -1; // since parameter positions are zero-based, return type pos is -1
-                       this.AttrsImpl = ParameterAttributes.Retval;
-                       this.marshalAs = marshalAs;
-               }
-
                public override string ToString() {
                        Type elementType = ClassImpl;
                        while (elementType.HasElementType) {
@@ -92,63 +78,61 @@ namespace System.Reflection
                        return result;
                }
 
+               internal static void FormatParameters (StringBuilder sb, ParameterInfo[] p)
+               {
+                       for (int i = 0; i < p.Length; ++i) {
+                               if (i > 0)
+                                       sb.Append (", ");
+
+                               Type pt = p[i].ParameterType;
+                               bool byref = pt.IsByRef;
+                               if (byref)
+                                       pt = pt.GetElementType ();
+
+                               if (Type.ShouldPrintFullName (pt))
+                                       sb.Append (pt.ToString ());
+                               else
+                                       sb.Append (pt.Name);
+
+                               if (byref)
+                                       sb.Append (" ByRef");
+                       }
+               }
+
                public virtual Type ParameterType {
                        get {return ClassImpl;}
                }
                public virtual ParameterAttributes Attributes {
                        get {return AttrsImpl;}
                }
-               public virtual object DefaultValue {
-                       get {return DefaultValueImpl;}
-               }
 
                public bool IsIn {
                        get {
-#if NET_2_0
                                return (Attributes & ParameterAttributes.In) != 0;
-#else
-                               return (AttrsImpl & ParameterAttributes.In) != 0;
-#endif
                        }
                }
 
                public bool IsLcid {
                        get {
-#if NET_2_0
                                return (Attributes & ParameterAttributes.Lcid) != 0;
-#else
-                               return (AttrsImpl & ParameterAttributes.Lcid) != 0;
-#endif
                        }
                }
 
                public bool IsOptional {
                        get {
-#if NET_2_0
                                return (Attributes & ParameterAttributes.Optional) != 0;
-#else
-                               return (AttrsImpl & ParameterAttributes.Optional) != 0;
-#endif
                        }
                }
 
                public bool IsOut {
                        get {
-#if NET_2_0
                                return (Attributes & ParameterAttributes.Out) != 0;
-#else
-                               return (AttrsImpl & ParameterAttributes.Out) != 0;
-#endif
                        }
                }
 
                public bool IsRetval {
                        get {
-#if NET_2_0
                                return (Attributes & ParameterAttributes.Retval) != 0;
-#else
-                               return (AttrsImpl & ParameterAttributes.Retval) != 0;
-#endif
                        }
                }
 
@@ -164,29 +148,8 @@ namespace System.Reflection
                        get {return PositionImpl;}
                }
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
-               public
-#else
-               internal
-#endif
-               virtual extern int MetadataToken {
-                       [MethodImplAttribute (MethodImplOptions.InternalCall)]
-                       get;
-               }
-
-               public virtual object[] GetCustomAttributes (bool inherit)
-               {
-                       return MonoCustomAttrs.GetCustomAttributes (this, inherit);
-               }
-
-               public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
-               {
-                       return MonoCustomAttrs.GetCustomAttributes (this, attributeType, inherit);
-               }
-
-               public virtual bool IsDefined( Type attributeType, bool inherit) {
-                       return MonoCustomAttrs.IsDefined (this, attributeType, inherit);
-               }
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal extern int GetMetadataToken ();
 
                internal object[] GetPseudoCustomAttributes () {
                        int count = 0;
@@ -213,45 +176,30 @@ namespace System.Reflection
                                attrs [count ++] = new OutAttribute ();
 
                        if (marshalAs != null)
-                               attrs [count ++] = marshalAs.ToMarshalAsAttribute ();
+                               attrs [count ++] = marshalAs.Copy ();
 
                        return attrs;
                }                       
 
-#if NET_2_0 || BOOTSTRAP_NET_2_0
-               [Obsolete ("Use ParameterInfo.GetOptionalCustomModifiers().")]
-               public virtual Type[] OptionalCustomModifiers {
-                       get {
-                               return GetOptionalCustomModifiers ();
-                       }
-               }
-
-               [Obsolete ("Use ParameterInfo.GetRequiredCustomModifiers().")]
-               public virtual Type[] RequiredCustomModifiers {
-                       get {
-                               return GetRequiredCustomModifiers ();
-                       }
-               }
+               [MethodImplAttribute (MethodImplOptions.InternalCall)]
+               internal extern Type[] GetTypeModifiers (bool optional);
 
-               [MonoTODO]
-               public virtual Type[] GetOptionalCustomModifiers () {
-                       throw new NotImplementedException ();
+               internal object GetDefaultValueImpl ()
+               {
+                       return DefaultValueImpl;
                }
 
-               [MonoTODO]
-               public virtual Type[] GetRequiredCustomModifiers () {
-                       throw new NotImplementedException ();
+#if NET_4_5
+               public virtual IEnumerable<CustomAttributeData> CustomAttributes {
+                       get { return GetCustomAttributesData (); }
                }
-
-               [MonoTODO]
-               public virtual object RawDefaultValue {
-                       get {
-                               throw new NotImplementedException ();
-                       }
+               
+               public virtual bool HasDefaultValue {
+                       get { throw new NotImplementedException (); }
                }
 #endif
 
-#if NET_1_1
+#if !MOBILE
                void _ParameterInfo.GetIDsOfNames ([In] ref Guid riid, IntPtr rgszNames, uint cNames, uint lcid, IntPtr rgDispId)
                {
                        throw new NotImplementedException ();
@@ -273,5 +221,83 @@ namespace System.Reflection
                        throw new NotImplementedException ();
                }
 #endif
+
+#if NET_4_0
+               public virtual object DefaultValue {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public virtual object RawDefaultValue {
+                       get { throw new NotImplementedException (); }
+               }
+
+               public virtual int MetadataToken {
+                       get { return 0x8000000; }
+               }
+
+               public virtual object[] GetCustomAttributes (bool inherit)
+               {
+                       return new object [0];
+               }
+
+               public virtual object[] GetCustomAttributes (Type attributeType, bool inherit)
+               {
+                       return new object [0];
+               }
+
+               public virtual bool IsDefined( Type attributeType, bool inherit) {
+                       return false;
+               }
+
+               public virtual Type[] GetRequiredCustomModifiers () {
+                       return new Type [0];
+               }
+
+               public virtual Type[] GetOptionalCustomModifiers () {
+                       return new Type [0];
+               }
+
+               public virtual IList<CustomAttributeData> GetCustomAttributesData () {
+                       throw new NotImplementedException ();
+               }
+#endif
+
+#if !FULL_AOT_RUNTIME
+               internal static ParameterInfo New (ParameterBuilder pb, Type type, MemberInfo member, int position)
+               {
+#if NET_4_0
+                       return new MonoParameterInfo (pb, type, member, position);
+#else
+                       return new ParameterInfo (pb, type, member, position);
+#endif
+               }
+#endif
+
+               internal static ParameterInfo New (ParameterInfo pinfo, Type type, MemberInfo member, int position)
+               {
+#if NET_4_0
+                       return new MonoParameterInfo (pinfo, type, member, position);
+#else
+                       return new ParameterInfo (pinfo, type, member, position);
+#endif
+               }
+
+               internal static ParameterInfo New (ParameterInfo pinfo, MemberInfo member)
+               {
+#if NET_4_0
+                       return new MonoParameterInfo (pinfo, member);
+#else
+                       return new ParameterInfo (pinfo, member);
+#endif
+               }
+
+               internal static ParameterInfo New (Type type, MemberInfo member, MarshalAsAttribute marshalAs)
+               {
+#if NET_4_0
+                       return new MonoParameterInfo (type, member, marshalAs);
+#else
+                       return new ParameterInfo (type, member, marshalAs);
+#endif 
+               }
        }
 }