Knaģis already provided the corresponding answer, but I wanted to indicate that this line of code is incorrect:
Type T =dt.Columns[0].DataType.GetType();
This will always return Type , not string , int , etc. This is because the DataType is equal to Type . Use this instead:
Type T =dt.Columns[0].DataType;
In addition, you use a naming convention for the generic type. Although this confuses the first reading a bit, nothing happens to it. Just keep in mind that there is a difference between a generic type and a variable. For example, enter the code in your question, you could not use this code:
var x = new List<T>;
In the context of your code, T is a variable, not a generic type, so the above will result in a compile-time error.
See this question for more information:
Generics in C # using variable type as parameter
source share