Reputation: 3157
I am trying to get the Type of a class which is generic. Most questions revolve around people trying to retrieve the Type T from a List(Of T)
for example. Instead I want the Type of the class itself, i.e. System.Collections.Generic.List
I am trying the following code, but it returns the Type including the generic parameter. If I then try to create an instance of the class with a different generic Type I get an error because the Type is closed.
'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)
So is there either a way to clear a generic parameter from a Type or some way of extracting the Type without Generic Parameters?
Upvotes: 0
Views: 534
Reputation: 64943
You can get opened one calling Type.GetGenericTypeDefinition
.
BTW, if you really know that you're going to create an instance of List<T>
, you can get the generic type definition using GetType(List(Of T))
.
Upvotes: 1