Skip to content

Commit

Permalink
[mono] Improve RuntimeConstructorInfo.ToString (#38451)
Browse files Browse the repository at this point in the history
* [mono] Improve RuntimeConstructorInfo.ToString

Use CoreCLR's code. Fixes #36688

* Use CoreCLR's code for RuntimeMethodInfo too
  • Loading branch information
alexischr authored Jul 15, 2020
1 parent 60cd838 commit 4e1ada2
Showing 1 changed file with 42 additions and 8 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -136,6 +136,7 @@ internal static ParameterInfo GetReturnParameterInfo(RuntimeMethodInfo method)
}
}

#region Sync with _MonoReflectionMethod in object-internals.h
[StructLayout(LayoutKind.Sequential)]
internal class RuntimeMethodInfo : MethodInfo
{
Expand All @@ -144,6 +145,8 @@ internal class RuntimeMethodInfo : MethodInfo
private string? name;
private Type? reftype;
#pragma warning restore 649
#endregion
private string? toString;

internal BindingFlags BindingFlags
{
Expand Down Expand Up @@ -194,9 +197,28 @@ public override Delegate CreateDelegate(Type delegateType, object? target)
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()
Expand Down Expand Up @@ -746,7 +768,7 @@ public override IList<CustomAttributeData> GetCustomAttributesData()

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
{
Expand All @@ -755,6 +777,8 @@ internal class RuntimeConstructorInfo : ConstructorInfo
private string? name;
private Type? reftype;
#pragma warning restore 649
#endregion
private string? toString;

public override Module Module
{
Expand Down Expand Up @@ -968,16 +992,26 @@ public override MethodBody GetMethodBody()
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()
Expand Down

0 comments on commit 4e1ada2

Please sign in to comment.