Get Type object representing a generic type from which the current generic type in CSharp
Description
The following code shows how to get Type object representing a generic type from which the current generic type.
Example
//from ww w.j a v a 2 s .com
using System;
using System.Reflection;
using System.Collections.Generic;
public class Test
{
public static void Main()
{
Dictionary<string, Test> d = new Dictionary<string, Test>();
Type constructed = d.GetType();
DisplayTypeInfo(constructed);
Type generic = constructed.GetGenericTypeDefinition();
DisplayTypeInfo(generic);
}
private static void DisplayTypeInfo(Type t)
{
Console.WriteLine(t.IsGenericTypeDefinition);
Console.WriteLine(t.IsGenericType);
Type[] typeArguments = t.GetGenericArguments();
Console.WriteLine(typeArguments.Length);
foreach (Type tParam in typeArguments)
{
Console.WriteLine("\t\t{0}", tParam);
}
}
}
The code above generates the following result.