-
-
Save dterracino/6f84f6faa2b1208f616e81a2acba4d52 to your computer and use it in GitHub Desktop.
Humanize C# generic types.
This file contains hidden or 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 string Humanize(this Type type, bool includeNamespace = false) | |
{ | |
// simply return its name | |
if (!type.IsGenericType) | |
return includeNamespace ? $"{type.Namespace}.{type.Name}" : type.Name; | |
// what are you doing | |
if (type.IsNested && type.DeclaringType.IsGenericType) | |
throw new NotImplementedException(); | |
// generics, but done correctly | |
// MyType<TypeOne, TypeTwo> | |
return $"{(includeNamespace ? $"{type.Namespace}." : "")}{type.Name.Substring(0, type.Name.IndexOf('`'))}<{string.Join(", ", type.GetGenericArguments().Select(x => x.Humanize(includeNamespace)))}>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment