[corlib] Update public api
[mono.git] / mcs / class / corlib / System.Reflection / MonoMethod.cs
index c503c399b37beffd3ba7f7b760b8ee3e01c56074..a7c66597453323b962c65be5b41c476855ed0453 100644 (file)
@@ -41,6 +41,7 @@ using System.Security;
 using System.Threading;
 using System.Text;
 using System.Diagnostics;
+using System.Diagnostics.Contracts;
 
 namespace System.Reflection {
        
@@ -109,13 +110,66 @@ namespace System.Reflection {
                }
        };
        
-       abstract class RuntimeMethodInfo : MethodInfo
+       abstract class RuntimeMethodInfo : MethodInfo, ISerializable
        {
                internal BindingFlags BindingFlags {
                        get {
                                return 0;
                        }
                }
+
+               RuntimeType ReflectedTypeInternal {
+                       get {
+                               return (RuntimeType) ReflectedType;
+                       }
+               }
+
+        internal override string FormatNameAndSig (bool serialization)
+        {
+            // Serialization uses ToString to resolve MethodInfo overloads.
+            StringBuilder sbName = new StringBuilder(Name);
+
+            // serialization == true: use unambiguous (except for assembly name) type names to distinguish between overloads.
+            // serialization == false: use basic format to maintain backward compatibility of MethodInfo.ToString().
+            TypeNameFormatFlags format = serialization ? TypeNameFormatFlags.FormatSerialization : TypeNameFormatFlags.FormatBasic;
+
+            if (IsGenericMethod)
+                sbName.Append(RuntimeMethodHandle.ConstructInstantiation(this, format));
+
+            sbName.Append("(");
+            ParameterInfo.FormatParameters (sbName, GetParametersNoCopy (), CallingConvention, serialization);
+            sbName.Append(")");
+
+            return sbName.ToString();
+        }
+
+        public override String ToString() 
+        {
+            return ReturnType.FormatTypeName() + " " + FormatNameAndSig(false);
+        }
+
+        #region ISerializable Implementation
+        public void GetObjectData(SerializationInfo info, StreamingContext context)
+        {
+            if (info == null)
+                throw new ArgumentNullException("info");
+            Contract.EndContractBlock();
+
+            MemberInfoSerializationHolder.GetSerializationInfo(
+                info,
+                Name,
+                ReflectedTypeInternal,
+                ToString(),
+                SerializationToString(),
+                MemberTypes.Method,
+                IsGenericMethod & !IsGenericMethodDefinition ? GetGenericArguments() : null);
+        }
+
+        internal string SerializationToString()
+        {
+            return ReturnType.FormatTypeName(true) + " " + FormatNameAndSig(true);
+        }
+        #endregion
        }
 
        /*
@@ -124,7 +178,7 @@ namespace System.Reflection {
         */
        [Serializable()]
        [StructLayout (LayoutKind.Sequential)]
-       internal class MonoMethod : RuntimeMethodInfo, ISerializable
+       internal class MonoMethod : RuntimeMethodInfo
        {
 #pragma warning disable 649
                internal IntPtr mhandle;
@@ -328,7 +382,7 @@ namespace System.Reflection {
                }
 
                [MethodImplAttribute(MethodImplOptions.InternalCall)]
-               internal static extern DllImportAttribute GetDllImportAttribute (IntPtr mhandle);
+               internal extern void GetPInvoke (out PInvokeAttributes flags, out string entryPoint, out string dllName);
 
                internal object[] GetPseudoCustomAttributes ()
                {
@@ -350,58 +404,12 @@ namespace System.Reflection {
                        if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
                                attrs [count ++] = new PreserveSigAttribute ();
                        if ((info.attrs & MethodAttributes.PinvokeImpl) != 0) {
-                               DllImportAttribute attr = GetDllImportAttribute (mhandle);
-                               if ((info.iattrs & MethodImplAttributes.PreserveSig) != 0)
-                                       attr.PreserveSig = true;
-                               attrs [count ++] = attr;
+                               attrs [count ++] = DllImportAttribute.GetCustomAttribute (this);
                        }
 
                        return attrs;
                }
 
-               public override string ToString () {
-                       StringBuilder sb = new StringBuilder ();
-                       Type retType = ReturnType;
-                       if (Type.ShouldPrintFullName (retType))
-                               sb.Append (retType.ToString ());
-                       else
-                               sb.Append (retType.Name);
-                       sb.Append (" ");
-                       sb.Append (Name);
-                       if (IsGenericMethod) {
-                               Type[] gen_params = GetGenericArguments ();
-                               sb.Append ("[");
-                               for (int j = 0; j < gen_params.Length; j++) {
-                                       if (j > 0)
-                                               sb.Append (",");
-                                       sb.Append (gen_params [j].Name);
-                               }
-                               sb.Append ("]");
-                       }
-                       sb.Append ("(");
-
-                       var p = GetParametersInternal ();
-                       ParameterInfo.FormatParameters (sb, p);
-
-                       if ((CallingConvention & CallingConventions.VarArgs) != 0) {
-                               if (p.Length > 0)
-                                       sb.Append (", ");
-                               sb.Append ("...");
-                       }
-                       
-                       sb.Append (")");
-                       return sb.ToString ();
-               }
-
-       
-               // ISerializable
-               public void GetObjectData(SerializationInfo info, StreamingContext context) 
-               {
-                       Type[] genericArguments = IsGenericMethod && !IsGenericMethodDefinition
-                               ? GetGenericArguments () : null;
-                       MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Method, genericArguments);
-               }
-
                public override MethodInfo MakeGenericMethod (Type [] methodInstantiation)
                {
                        if (methodInstantiation == null)
@@ -481,21 +489,82 @@ namespace System.Reflection {
                public override IList<CustomAttributeData> GetCustomAttributesData () {
                        return CustomAttributeData.GetCustomAttributes (this);
                }
+
+               //seclevel { transparent = 0, safe-critical = 1, critical = 2}
+               [MethodImplAttribute(MethodImplOptions.InternalCall)]
+               public extern int get_core_clr_security_level ();
+
+               public override bool IsSecurityTransparent {
+                       get { return get_core_clr_security_level () == 0; }
+               }
+
+               public override bool IsSecurityCritical {
+                       get { return get_core_clr_security_level () > 0; }
+               }
+
+               public override bool IsSecuritySafeCritical {
+                       get { return get_core_clr_security_level () == 1; }
+               }
        }
        
 
-       abstract class RuntimeConstructorInfo : ConstructorInfo
+       abstract class RuntimeConstructorInfo : ConstructorInfo, ISerializable
        {
+               public override Module Module {
+                       get {
+                               return GetRuntimeModule ();
+                       }
+               }
+
+               internal RuntimeModule GetRuntimeModule ()
+               {
+                       return RuntimeTypeHandle.GetModule((RuntimeType)DeclaringType);
+               }
+
                internal BindingFlags BindingFlags {
                        get {
                                return 0;
                        }
                }
+
+               RuntimeType ReflectedTypeInternal {
+                       get {
+                               return (RuntimeType) ReflectedType;
+                       }
+               }
+
+        #region ISerializable Implementation
+        public void GetObjectData(SerializationInfo info, StreamingContext context)
+        {
+            if (info == null)
+                throw new ArgumentNullException("info");
+            Contract.EndContractBlock();
+            MemberInfoSerializationHolder.GetSerializationInfo(
+                info,
+                Name,
+                ReflectedTypeInternal,
+                ToString(),
+                SerializationToString(),
+                MemberTypes.Constructor,
+                null);
+        }
+
+        internal string SerializationToString()
+        {
+            // We don't need the return type for constructors.
+            return FormatNameAndSig(true);
+        }
+
+               internal void SerializationInvoke (Object target, SerializationInfo info, StreamingContext context)
+               {
+                       Invoke (target, new object[] { info, context });
+               }
+       #endregion
        }
 
        [Serializable()]
        [StructLayout (LayoutKind.Sequential)]
-       internal class MonoCMethod : RuntimeConstructorInfo, ISerializable
+       internal class MonoCMethod : RuntimeConstructorInfo
        {
 #pragma warning disable 649            
                internal IntPtr mhandle;
@@ -676,12 +745,6 @@ namespace System.Reflection {
                        return sb.ToString ();
                }
 
-               // ISerializable
-               public void GetObjectData(SerializationInfo info, StreamingContext context) 
-               {
-                       MemberInfoSerializationHolder.Serialize ( info, Name, ReflectedType, ToString(), MemberTypes.Constructor);
-               }
-
                public override IList<CustomAttributeData> GetCustomAttributesData () {
                        return CustomAttributeData.GetCustomAttributes (this);
                }