Move FormatTypeName to Type (dotnet/coreclr#25631)
authorEgor Bogatov <egorbo@gmail.com>
Wed, 28 Aug 2019 18:47:38 +0000 (21:47 +0300)
committerJan Kotas <jkotas@microsoft.com>
Wed, 28 Aug 2019 18:47:38 +0000 (11:47 -0700)
Commit migrated from https://github.com/dotnet/coreclr/commit/f07c13cadc378649f10d024bb8a40a3878fa661f

src/coreclr/src/System.Private.CoreLib/src/System/Reflection/MethodBase.CoreCLR.cs
src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
src/coreclr/src/System.Private.CoreLib/src/System/Type.CoreCLR.cs
src/libraries/System.Private.CoreLib/src/System/Reflection/MethodBase.cs
src/libraries/System.Private.CoreLib/src/System/Type.cs

index 5d830df..34dc255 100644 (file)
@@ -53,43 +53,6 @@ namespace System.Reflection
         #region Internal Methods
         // helper method to construct the string representation of the parameter list
 
-        internal const int MethodNameBufferSize = 100;
-
-        internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] parameterTypes, CallingConventions callingConvention)
-        {
-            string comma = "";
-
-            for (int i = 0; i < parameterTypes.Length; i++)
-            {
-                Type t = parameterTypes[i];
-
-                sbParamList.Append(comma);
-
-                string typeName = t.FormatTypeName();
-
-                // Legacy: Why use "ByRef" for by ref parameters? What language is this?
-                // VB uses "ByRef" but it should precede (not follow) the parameter name.
-                // Why don't we just use "&"?
-                if (t.IsByRef)
-                {
-                    sbParamList.Append(typeName.AsSpan().TrimEnd('&'));
-                    sbParamList.Append(" ByRef");
-                }
-                else
-                {
-                    sbParamList.Append(typeName);
-                }
-
-                comma = ", ";
-            }
-
-            if ((callingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs)
-            {
-                sbParamList.Append(comma);
-                sbParamList.Append("...");
-            }
-        }
-
         internal virtual Type[] GetParameterTypes()
         {
             ParameterInfo[] paramInfo = GetParametersNoCopy();
index e288db0..e93bbc1 100644 (file)
@@ -3864,35 +3864,6 @@ namespace System
 
         public override string Name => GetCachedName(TypeNameKind.Name)!;
 
-        // This is used by the ToString() overrides of all reflection types. The legacy behavior has the following problems:
-        //  1. Use only Name for nested types, which can be confused with global types and generic parameters of the same name.
-        //  2. Use only Name for generic parameters, which can be confused with nested types and global types of the same name.
-        //  3. Remove the namespace ("System") for all primitive types, which is not language neutral.
-        //  4. MethodBase.ToString() use "ByRef" for byref parameters which is different than Type.ToString().
-        //  5. ConstructorInfo.ToString() outputs "Void" as the return type. Why Void?
-        internal override string FormatTypeName()
-        {
-            Type elementType = GetRootElementType();
-
-            // Legacy: this doesn't make sense, why use only Name for nested types but otherwise
-            // ToString() which contains namespace.
-            if (elementType.IsNested)
-                return Name;
-
-            string typeName = ToString();
-
-            // Legacy: why removing "System"? Is it just because C# has keywords for these types?
-            // If so why don't we change it to lower case to match the C# keyword casing?
-            if (elementType.IsPrimitive ||
-                elementType == typeof(void) ||
-                elementType == typeof(TypedReference))
-            {
-                typeName = typeName.Substring(@"System.".Length);
-            }
-
-            return typeName;
-        }
-
         // This method looks like an attractive inline but expands to two calls,
         // neither of which can be inlined or optimized further. So block it
         // from inlining.
index 68a5a2c..9823d5c 100644 (file)
@@ -140,12 +140,6 @@ namespace System
         }
 #endif // FEATURE_COMINTEROP
 
-        // This is only ever called on RuntimeType objects.
-        internal virtual string FormatTypeName()
-        {
-            throw new NotImplementedException();
-        }
-
         [MethodImplAttribute(MethodImplOptions.InternalCall)]
         public static extern bool operator ==(Type? left, Type? right);
 
index a2fa801..d737ccf 100644 (file)
@@ -5,6 +5,7 @@
 using System.Globalization;
 using System.Diagnostics;
 using System.Runtime.CompilerServices;
+using System.Text;
 
 namespace System.Reflection
 {
@@ -79,5 +80,42 @@ namespace System.Reflection
         }
 
         public static bool operator !=(MethodBase? left, MethodBase? right) => !(left == right);
+
+        internal const int MethodNameBufferSize = 100;
+
+        internal static void AppendParameters(ref ValueStringBuilder sbParamList, Type[] parameterTypes, CallingConventions callingConvention)
+        {
+            string comma = "";
+
+            for (int i = 0; i < parameterTypes.Length; i++)
+            {
+                Type t = parameterTypes[i];
+
+                sbParamList.Append(comma);
+
+                string typeName = t.FormatTypeName();
+
+                // Legacy: Why use "ByRef" for by ref parameters? What language is this?
+                // VB uses "ByRef" but it should precede (not follow) the parameter name.
+                // Why don't we just use "&"?
+                if (t.IsByRef)
+                {
+                    sbParamList.Append(typeName.AsSpan().TrimEnd('&'));
+                    sbParamList.Append(" ByRef");
+                }
+                else
+                {
+                    sbParamList.Append(typeName);
+                }
+
+                comma = ", ";
+            }
+
+            if ((callingConvention & CallingConventions.VarArgs) == CallingConventions.VarArgs)
+            {
+                sbParamList.Append(comma);
+                sbParamList.Append("...");
+            }
+        }
     }
 }
index db03ec4..c525497 100644 (file)
@@ -352,6 +352,25 @@ namespace System
             return new SignatureGenericMethodParameterType(position);
         }
 
+        // This is used by the ToString() overrides of all reflection types. The legacy behavior has the following problems:
+        //  1. Use only Name for nested types, which can be confused with global types and generic parameters of the same name.
+        //  2. Use only Name for generic parameters, which can be confused with nested types and global types of the same name.
+        //  3. Use only Name for all primitive types, void and TypedReference
+        //  4. MethodBase.ToString() use "ByRef" for byref parameters which is different than Type.ToString().
+        //  5. ConstructorInfo.ToString() outputs "Void" as the return type. Why Void?
+        internal string FormatTypeName()
+        {
+            Type elementType = GetRootElementType();
+
+            if (elementType.IsPrimitive ||
+                elementType.IsNested ||
+                elementType == typeof(void) ||
+                elementType == typeof(TypedReference))
+                return Name;
+
+            return ToString();
+        }
+
         public override string ToString() => "Type: " + Name;  // Why do we add the "Type: " prefix?
 
         public override bool Equals(object? o) => o == null ? false : Equals(o as Type);