泛型类型参数和类型

wdh*_*ugh 1 c# generics types

我有一些代码如下所示.我想知道是否可以为传递给DoSomething函数的泛型类型获取Type对象.我意识到T是一个类型参数,但我如何将其转换为类型对象.dosomething函数在一个类中,我想知道列表对象使用的基础类型.

[STAThread]
static void Main(string[] args)
{
    List<string> stringList = new List<string>();
    DoSomething(stringList); 
}
public void DoSomething<T>(List<T> collection)
{
    //Type myType = T; ??!!? 
    //do something here with the list.
}
Run Code Online (Sandbox Code Playgroud)

Wim*_*dse 12

只需使用:

Type type = typeof(T);
Run Code Online (Sandbox Code Playgroud)

通用参数与任何其他类型一样,除了您不知道该类型在编译时到底是什么,因此假设它是对象层次结构中最重要的,它符合给定的类型约束(对于没有约束的System.Object).