Skip to content

Instantly share code, notes, and snippets.

@aalmada
Created January 4, 2019 14:14
Show Gist options
  • Save aalmada/f2c69949cba590ad36a8120a5f40d17b to your computer and use it in GitHub Desktop.
Save aalmada/f2c69949cba590ad36a8120a5f40d17b to your computer and use it in GitHub Desktop.
An extension-method for System.Type that returns its human-readable name.
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