Skip to content

Instantly share code, notes, and snippets.

@leandromoh
Created April 20, 2022 20:12
Show Gist options
  • Save leandromoh/c75b0582f15a06f50484a133e66d4b47 to your computer and use it in GitHub Desktop.
Save leandromoh/c75b0582f15a06f50484a133e66d4b47 to your computer and use it in GitHub Desktop.
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