CSharp - typeof Unbound Generic Types

Introduction

You can get the type of an unbound generic type using typeof operator:

class A<T> {}
class A<T1,T2> {}

Type a1 = typeof (A<>);   // Unbound type (notice no type arguments).
Type a2 = typeof (A<,>);  // Use commas to indicate multiple type args.

The type of an unbound generic type are used in the Reflection API.

You can use the typeof operator to specify a closed type:

Type a3 = typeof (A<int,int>);

or an open type (which is closed at runtime):

class GenericClass<T> { 
   void MyMethod() { 
      Type t = typeof (T); //check the type of your filler type
      Console.WriteLine("you are using "+ t);
   } 
}

Demo

using System;
class A<T> {
}
class A<T1,T2> {
}
class MainClass{/*from ww w  . j a  v  a 2  s.  co m*/
   public static void Main(string[] args)   {
        Type a1 = typeof (A<>);  
        Console.WriteLine(a1);
        Type a2 = typeof (A<,>); 
        Console.WriteLine(a2);
   }
}

Result