Here is an extension method that will get the "real" name of a generic type along with the names of the generic type parameters. It will properly handle nested generic types.
public static class MyExtensionMethods
{
public static string GetRealTypeName(this Type t)
{
if (!t.IsGenericType)
return t.Name;
StringBuilder sb = new StringBuilder();
sb.Append(t.Name.Substring(0, t.Name.IndexOf('`')));
sb.Append('<');
bool appendComma = false;
foreach (Type arg in t.GetGenericArguments())
{
if (appendComma) sb.Append(',');
sb.Append(GetRealTypeName(arg));
appendComma = true;
}
sb.Append('>');
return sb.ToString();
}
}
Here is a sample program showing its usage:
static void Main(string[] args)
{
Console.WriteLine(typeof(int).GetRealTypeName());
Console.WriteLine(typeof(List<string>).GetRealTypeName());
Console.WriteLine(typeof(long?).GetRealTypeName());
Console.WriteLine(typeof(Dictionary<int, List<string>>).GetRealTypeName());
Console.WriteLine(typeof(Func<List<Dictionary<string, object>>, bool>).GetRealTypeName());
}
And here is the output of the above program:
Int32
List<String>
Nullable<Int64>
Dictionary<Int32,List<String>>
Func<List<Dictionary<String,Object>>,Boolean>