-1

我有一堂课:

public class test<T> {}

我想得到类的类型。我有通用类型。我需要使用 typeof。

这行得通吗?

Type genericType = typeof(System.Int32);
typeof(test<>).MakeGenericType(new Type[] { genericType })

提前谢谢你的帮助

4

1 回答 1

1

这行得通吗?

是的(假设您在行尾添加了正确的分号,并将结果值分配给System.Type变量)。

例如,给定:

Type genericType = typeof(System.Int32);
Type fullType = typeof(test<>).MakeGenericType(new Type[] { genericType });
Console.WriteLine(fullType);

这将打印:

MyNamespace.test`1[System.Int32]

这是完全限定类型的正确类型名称。

于 2013-08-28T00:15:11.897 回答