Skip to content

Commit

Permalink
Move FormatTypeName to Type (dotnet/coreclr#25631)
Browse files Browse the repository at this point in the history
Commit migrated from dotnet/coreclr@f07c13c
  • Loading branch information
EgorBo authored and jkotas committed Aug 28, 2019
1 parent 52447da commit c06a38b
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 72 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -53,43 +53,6 @@ public abstract partial class MethodBase : MemberInfo
#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();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3864,35 +3864,6 @@ public override Type MakeArrayType(int rank)

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.
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -140,12 +140,6 @@ internal virtual bool IsExportedToWindowsRuntimeImpl()
}
#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);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
using System.Globalization;
using System.Diagnostics;
using System.Runtime.CompilerServices;
using System.Text;

namespace System.Reflection
{
Expand Down Expand Up @@ -79,5 +80,42 @@ this is ConstructorInfo &&
}

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("...");
}
}
}
}
19 changes: 19 additions & 0 deletions src/libraries/System.Private.CoreLib/src/System/Type.cs
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,25 @@ public static Type MakeGenericMethodParameter(int position)
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);
Expand Down

0 comments on commit c06a38b

Please sign in to comment.