C# 如果类具有泛型参数,则使用typeof获取类类型

C# 如果类具有泛型参数,则使用typeof获取类类型,c#,generics,reflection,C#,Generics,Reflection,我有一门课: public class test<T> {} 公共类测试{} 我想知道班级的类型。我有泛型。我需要使用typeof 这样行吗 Type genericType = typeof(System.Int32); typeof(test<>).MakeGenericType(new Type[] { genericType }) Type genericType=typeof(System.Int32); typeof(test).MakeGenericT

我有一门课:

public class test<T> {}
公共类测试{}
我想知道班级的类型。我有泛型。我需要使用typeof

这样行吗

Type genericType = typeof(System.Int32);
typeof(test<>).MakeGenericType(new Type[] { genericType })
Type genericType=typeof(System.Int32);
typeof(test).MakeGenericType(新类型[]{genericType})
提前谢谢你的帮助

这样行吗

Type genericType = typeof(System.Int32);
typeof(test<>).MakeGenericType(new Type[] { genericType })
是(假设在行尾添加适当的分号,并将结果值分配给
System.Type
变量)

例如,假设:

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

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

是的。我实际上是在反射中使用它,但我不能100%确定它是否正常工作。最初的问题实际上是一个通用接口。问题是:这是获得类型的正确方法吗?。。。那问题是什么。。。因为您所尝试的方法适用于您所问的内容。我问这个问题的原因是因为我看到类似于:MyNamespace.test`1[[System.Int32]]。