Created
April 20, 2022 20:12
-
-
Save leandromoh/c75b0582f15a06f50484a133e66d4b47 to your computer and use it in GitHub Desktop.
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
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
Console.WriteLine(GetFormattedName(typeof(int))); | |
Console.WriteLine(GetFormattedName(typeof(decimal?))); | |
Console.WriteLine(GetFormattedName(typeof(IEnumerable<List<string>>))); | |
Console.WriteLine(GetFormattedName(typeof(Dictionary<int, string>))); | |
static string GetFormattedName(Type type) | |
{ | |
if (!type.IsGenericType) | |
return type.Name; | |
var names = type.GetGenericArguments().Select(GetFormattedName); | |
var genericArguments = string.Join(", ", names); | |
var underlyingType = type.Name.Substring(0, type.Name.IndexOf("`")); | |
return $"{underlyingType}<{genericArguments}>"; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment