Skip to content

Commit

Permalink
Align reflection name formatting with CoreCLR-JIT (#102756)
Browse files Browse the repository at this point in the history
There was an observable difference between FormatTypeNameForReflection and FormatTypeName for TypedReference.
  • Loading branch information
MichalStrehovsky authored May 29, 2024
1 parent ec5963d commit 3eb94d1
Show file tree
Hide file tree
Showing 7 changed files with 7 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ public sealed override string ToString()
ComputeTypedArgumentString(namedArgument.TypedValue, typed));
}

return string.Format("[{0}({1}{2})]", AttributeType.FormatTypeNameForReflection(), ctorArgs, namedArgs);
return string.Format("[{0}({1}{2})]", AttributeType.FormatTypeName(), ctorArgs, namedArgs);
}

protected static ConstructorInfo ResolveAttributeConstructor(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -114,7 +114,7 @@ public sealed override string ToString()
if (parameters.Length == 0)
throw new InvalidOperationException(); // Legacy: Why is a ToString() intentionally throwing an exception?
RuntimeParameterInfo runtimeParameterInfo = (RuntimeParameterInfo)(parameters[0]);
return runtimeParameterInfo.ParameterType.FormatTypeNameForReflection() + " " + this.Name;
return runtimeParameterInfo.ParameterType.FormatTypeName() + " " + this.Name;
}

protected RuntimeEventInfo WithDebugName()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ protected sealed override string MetadataName

public sealed override string ToString()
{
return FieldRuntimeType.ToType().FormatTypeNameForReflection() + " " + this.Name;
return FieldRuntimeType.ToType().FormatTypeName() + " " + this.Name;
}

public sealed override bool HasSameMetadataDefinitionAs(MemberInfo other)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,7 @@ internal static string ComputeParametersString(RuntimeParameterInfo[] parameters
{
if (i != 0)
sb.Append(", ");
string parameterTypeString = parameters[i].ParameterType.FormatTypeNameForReflection();
string parameterTypeString = parameters[i].ParameterType.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.
Expand All @@ -94,7 +94,7 @@ internal static string ComputeParametersString(RuntimeParameterInfo[] parameters
internal static string ComputeToString(MethodBase contextMethod, RuntimeTypeInfo[] methodTypeArguments, RuntimeParameterInfo[] parameters, RuntimeParameterInfo returnParameter)
{
StringBuilder sb = new StringBuilder(30);
sb.Append(returnParameter == null ? "Void" : returnParameter.ParameterType.FormatTypeNameForReflection()); // ConstructorInfos allowed to pass in null rather than craft a ReturnParameterInfo that's always of type void.
sb.Append(returnParameter == null ? "Void" : returnParameter.ParameterType.FormatTypeName()); // ConstructorInfos allowed to pass in null rather than craft a ReturnParameterInfo that's always of type void.
sb.Append(' ');
sb.Append(contextMethod.Name);
if (methodTypeArguments.Length != 0)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ public sealed override int Position

public sealed override string ToString()
{
return this.ParameterType.FormatTypeNameForReflection() + " " + this.Name;
return this.ParameterType.FormatTypeName() + " " + this.Name;
}

private readonly MemberInfo _member;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ public sealed override string ToString()
{
StringBuilder sb = new StringBuilder(30);

sb.Append(PropertyType.FormatTypeNameForReflection());
sb.Append(PropertyType.FormatTypeName());
sb.Append(' ');
sb.Append(this.Name);
ParameterInfo[] indexParameters = this.GetIndexParameters();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -56,35 +56,6 @@ private static unsafe RuntimeType GetTypeFromMethodTableSlow(MethodTable* pMT)
}
}

//
// This is a port of the desktop CLR's RuntimeType.FormatTypeName() routine. This routine is used by various Reflection ToString() methods
// to display the name of a type. Do not use for any other purpose as it inherits some pretty quirky desktop behavior.
//
internal string FormatTypeNameForReflection()
{
// Legacy: this doesn't make sense, why use only Name for nested types but otherwise
// ToString() which contains namespace.
Type rootElementType = this;
while (rootElementType.HasElementType)
rootElementType = rootElementType.GetElementType()!;
if (rootElementType.IsNested)
{
return Name!;
}

// 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?
string typeName = ToString();
if (typeName.StartsWith("System."))
{
if (rootElementType.IsPrimitive || rootElementType == typeof(void))
{
typeName = typeName.Substring("System.".Length);
}
}
return typeName;
}

[Intrinsic]
[RequiresUnreferencedCode("The type might be removed")]
public static Type GetType(string typeName) => GetType(typeName, throwOnError: false, ignoreCase: false);
Expand Down

0 comments on commit 3eb94d1

Please sign in to comment.