[mono] Improve RuntimeConstructorInfo.ToString (#38451)
authorAlexis Christoforides <alexis@thenull.net>
Wed, 15 Jul 2020 18:40:46 +0000 (14:40 -0400)
committerGitHub <noreply@github.com>
Wed, 15 Jul 2020 18:40:46 +0000 (14:40 -0400)
* [mono] Improve RuntimeConstructorInfo.ToString

Use CoreCLR's code. Fixes https://github.com/dotnet/runtime/issues/36688

* Use CoreCLR's code for RuntimeMethodInfo too

src/mono/netcore/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.cs

index 6a72a6e4735c99fa23b748786d24027c576b1216..af61aab2ee9868d89b1b6d37877d7a6a294ff38e 100644 (file)
@@ -136,6 +136,7 @@ namespace System.Reflection
         }
     }
 
+#region Sync with _MonoReflectionMethod in object-internals.h
     [StructLayout(LayoutKind.Sequential)]
     internal class RuntimeMethodInfo : MethodInfo
     {
@@ -144,6 +145,8 @@ namespace System.Reflection
         private string? name;
         private Type? reftype;
 #pragma warning restore 649
+#endregion
+        private string? toString;
 
         internal BindingFlags BindingFlags
         {
@@ -194,9 +197,28 @@ namespace System.Reflection
             return Delegate.CreateDelegate(delegateType, target, this);
         }
 
+        // copied from CoreCLR's RuntimeMethodInfo
         public override string ToString()
         {
-            return ReturnType.FormatTypeName() + " " + FormatNameAndSig();
+            if (toString == null)
+            {
+                var sbName = new ValueStringBuilder(MethodNameBufferSize);
+
+                sbName.Append(ReturnType.FormatTypeName());
+                sbName.Append(' ');
+                sbName.Append(Name);
+
+                if (IsGenericMethod)
+                    sbName.Append(RuntimeMethodHandle.ConstructInstantiation(this, TypeNameFormatFlags.FormatBasic));
+
+                sbName.Append('(');
+                AppendParameters(ref sbName, GetParameterTypes(), CallingConvention);
+                sbName.Append(')');
+
+                toString = sbName.ToString();
+            }
+
+            return toString;
         }
 
         internal RuntimeModule GetRuntimeModule()
@@ -746,7 +768,7 @@ namespace System.Reflection
 
         public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other) => HasSameMetadataDefinitionAsCore<RuntimeMethodInfo>(other);
     }
-
+#region Sync with _MonoReflectionMethod in object-internals.h
     [StructLayout(LayoutKind.Sequential)]
     internal class RuntimeConstructorInfo : ConstructorInfo
     {
@@ -755,6 +777,8 @@ namespace System.Reflection
         private string? name;
         private Type? reftype;
 #pragma warning restore 649
+#endregion
+        private string? toString;
 
         public override Module Module
         {
@@ -968,16 +992,26 @@ namespace System.Reflection
             return RuntimeMethodInfo.GetMethodBody(mhandle);
         }
 
+        // copied from CoreCLR's RuntimeConstructorInfo
         public override string ToString()
         {
-            StringBuilder sbName = new StringBuilder(Name);
-            sbName.Append("Void ");
+            if (toString == null)
+            {
+                var sbName = new ValueStringBuilder(MethodNameBufferSize);
 
-            sbName.Append('(');
-            RuntimeParameterInfo.FormatParameters(sbName, GetParametersNoCopy(), CallingConvention);
-            sbName.Append(')');
+                // "Void" really doesn't make sense here. But we'll keep it for compat reasons.
+                sbName.Append("Void ");
 
-            return sbName.ToString();
+                sbName.Append(Name);
+
+                sbName.Append('(');
+                AppendParameters(ref sbName, GetParameterTypes(), CallingConvention);
+                sbName.Append(')');
+
+                toString = sbName.ToString();
+            }
+
+            return toString;
         }
 
         public override IList<CustomAttributeData> GetCustomAttributesData()