Skip to content

Commit

Permalink
Fix #3057: CSharpLanguage.ToCSharpString usage of StringBuilder not o…
Browse files Browse the repository at this point in the history
…ptimal
  • Loading branch information
siegfriedpammer committed Oct 22, 2023
1 parent 231a2dc commit e2ed67d
Showing 1 changed file with 26 additions and 12 deletions.
38 changes: 26 additions & 12 deletions ILSpy/Languages/CSharpLanguage.cs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
using System.Reflection;
using System.Reflection.Metadata;
using System.Reflection.PortableExecutable;
using System.Text;
using System.Windows;
using System.Windows.Controls;

Expand Down Expand Up @@ -612,42 +611,57 @@ public override string EventToString(IEvent @event, bool includeDeclaringTypeNam
return EntityToString(@event, includeDeclaringTypeName, includeNamespace, includeNamespaceOfDeclaringTypeName);
}

string ToCSharpString(MetadataReader metadata, TypeDefinitionHandle handle, bool fullName, bool omitGenerics)
static string ToCSharpString(MetadataReader metadata, TypeDefinitionHandle handle, bool fullName, bool omitGenerics)
{
StringBuilder builder = new StringBuilder();
var currentTypeDefHandle = handle;
var typeDef = metadata.GetTypeDefinition(currentTypeDefHandle);
List<string> builder = new List<string>();

while (!currentTypeDefHandle.IsNil)
{
if (builder.Length > 0)
builder.Insert(0, '.');
if (builder.Count > 0)
builder.Add(".");
typeDef = metadata.GetTypeDefinition(currentTypeDefHandle);
var part = ReflectionHelper.SplitTypeParameterCountFromReflectionName(metadata.GetString(typeDef.Name), out int typeParamCount);
var genericParams = typeDef.GetGenericParameters();
if (!omitGenerics && genericParams.Count > 0)
{
builder.Insert(0, '>');
builder.Add(">");
int firstIndex = genericParams.Count - typeParamCount;
for (int i = genericParams.Count - 1; i >= genericParams.Count - typeParamCount; i--)
{
builder.Insert(0, metadata.GetString(metadata.GetGenericParameter(genericParams[i]).Name));
builder.Insert(0, i == firstIndex ? '<' : ',');
builder.Add(metadata.GetString(metadata.GetGenericParameter(genericParams[i]).Name));
builder.Add(i == firstIndex ? "<" : ",");
}
}
builder.Insert(0, part);
builder.Add(part);
currentTypeDefHandle = typeDef.GetDeclaringType();
if (!fullName)
break;
}

if (fullName && !typeDef.Namespace.IsNil)
{
builder.Insert(0, '.');
builder.Insert(0, metadata.GetString(typeDef.Namespace));
builder.Add(".");
builder.Add(metadata.GetString(typeDef.Namespace));
}

return builder.ToString();
switch (builder.Count)
{
case 0:
return string.Empty;
case 1:
return builder[0];
case 2:
return builder[1] + builder[0];
case 3:
return builder[2] + builder[1] + builder[0];
case 4:
return builder[3] + builder[2] + builder[1] + builder[0];
default:
builder.Reverse();
return string.Concat(builder);
}
}

public override string GetEntityName(PEFile module, EntityHandle handle, bool fullName, bool omitGenerics)
Expand Down

0 comments on commit e2ed67d

Please # to comment.