在C#中,typeof關(guān)鍵字通常用于獲取指定類型的Type對(duì)象。在泛型編程中,可以使用typeof關(guān)鍵字來(lái)獲取泛型類型的Type對(duì)象。例如:
public class GenericClass<T>
{
public void PrintType()
{
Type type = typeof(T);
Console.WriteLine("Type of T is: " + type);
}
}
class Program
{
static void Main()
{
GenericClass<int> genericClass = new GenericClass<int>();
genericClass.PrintType(); // 輸出:Type of T is: System.Int32
}
}
在上面的示例中,我們定義了一個(gè)泛型類GenericClass