Mega Code Archive

 
Categories / C# / Reflection
 

Gets a value indicating whether the current Type object has type parameters that have not been replaced by specific types

using System; using System.Reflection; using System.Collections.Generic; public class Base<T, U> { } public class Derived<V> : Base<int, V> { } public class Test {     public static void Main()     {         Type derivedType = typeof(Derived<>);         DisplayGenericTypeInfo(derivedType);         DisplayGenericTypeInfo(derivedType.BaseType);     }     private static void DisplayGenericTypeInfo(Type t)     {         Console.WriteLine("\r\n{0}", t);         Console.WriteLine("\tIs this a generic type definition? {0}", t.IsGenericTypeDefinition);         Console.WriteLine("\tIs it a generic type? {0}", t.IsGenericType);         Console.WriteLine("\tDoes it have unassigned generic parameters? {0}", t.ContainsGenericParameters);         if (t.IsGenericType)         {             Type[] typeArguments = t.GetGenericArguments();             Console.WriteLine("\tList type arguments ({0}):", typeArguments.Length);             foreach (Type tParam in typeArguments)             {                 if (tParam.IsGenericParameter)                 {                     Console.WriteLine(                         "\t\t{0}  (unassigned - parameter position {1})",                         tParam,                         tParam.GenericParameterPosition);                 }                 else                 {                     Console.WriteLine("\t\t{0}", tParam);                 }             }         }     } }