Last active
March 31, 2021 09:33
-
-
Save jRimbault/8be2d8b56c28390df06b2fcbefc75ab9 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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