0
我想获得泛型类的类型。例如,大多数问题都是围绕尝试从List(Of T)
检索Type T的人员进行的。相反,我想类的类本身,即System.Collections.Generic.List从泛型类(T)获取类的类型
我想下面的代码,但它返回类型,包括泛型参数。如果我然后尝试使用不同的泛型类型创建类的实例,由于Type已关闭,因此我得到一个错误。
'The type I want to use a a Generic Parameter in a new object
dim O = MasterObject
'This is the class which is Generic
dim BaseType as Type = me.getType
'This is the line that errors, since BaseType already has a Generic Type
dim GenericType as Type=BaseType.MakeGenericType(O.getType())
'This would be the new object, with a new generic type
dim NewObject as Object = Activator.CreateInstance(GenericType,vars)
那么有没有一种方法来清除类型的一般参数或某种方式来提取没有通用参数的类型?
@CodeCaster是的...我编辑它。我认为OP认为实际类型只是'List';( –
谢谢!对不起,应该清楚我的List的例子只是一个例子...实际上,我的类型将是许多类型的继承子类之一....但你的答案完美无瑕,我可以在2分钟内将它标记为正确:-) – JLo
@JLo我很高兴它的工作;)不客气! –