Skip to content

Instantly share code, notes, and snippets.

@jRimbault
Last active March 31, 2021 09:33
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jRimbault/8be2d8b56c28390df06b2fcbefc75ab9 to your computer and use it in GitHub Desktop.
Save jRimbault/8be2d8b56c28390df06b2fcbefc75ab9 to your computer and use it in GitHub Desktop.
public static class TypeExt
{
public static string HumanReadableName(this Type self)
{
return InnerHumanReadable(self).ToString();
}
private static System.Text.StringBuilder InnerHumanReadable(Type self)
{
if (!self.IsGenericType)
{
return new(self.Name);
}
string GenericName(string name) => name.Substring(0, name.LastIndexOf('`'));
var builder = new System.Text.StringBuilder(GenericName(self.Name));
builder.Append("<");
// I should be able to use a stack here to avoid recursion
builder.Append(string.Join(",", self.GetGenericArguments().Select(InnerHumanReadable)));
builder.Append(">");
return builder;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment