Created
January 4, 2019 14:14
-
-
Save aalmada/f2c69949cba590ad36a8120a5f40d17b to your computer and use it in GitHub Desktop.
An extension-method for System.Type that returns its human-readable name.
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 partial class TypeExtensions | |
{ | |
public static string GetFriendlyName(this Type type) | |
{ | |
if (!type.IsGenericType) | |
return type.FullName; | |
var name = type.FullName.Substring(0, type.FullName.IndexOf('`')); | |
var fullName = new StringBuilder(name); | |
fullName.Append("<"); | |
var arguments = type.GenericTypeArguments; | |
for(int index = 0, end = arguments.Length; index < end; index++) | |
{ | |
if (index != 0) | |
fullName.Append(","); | |
fullName.Append(GetFriendlyName(arguments[index])); | |
} | |
fullName.Append(">"); | |
return fullName.ToString(); | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment