Mega Code Archive

 
Categories / C# Book / 09 Reflection
 

0622 Generic Types

The MakeGenericType method converts an unbound into a closed generic type. using System; using System.Reflection; using System.Collections.Generic; class Program { static void Main() { Type unbound = typeof(List<>); Type closed = unbound.MakeGenericType(typeof(int)); Type unbound2 = closed.GetGenericTypeDefinition(); // unbound == unbound2 } } IsGenericType property is true if a Type is generic The following tests whether a type is a nullable value type: using System; using System.Reflection; using System.Collections.Generic; class Program { static void Main() { Type nullable = typeof(bool?); Console.WriteLine(nullable.IsGenericType && nullable.GetGenericTypeDefinition() == typeof(Nullable<>)); } }