These methods are helpful when you already have a System.Type instance representing a
closed type. However, the method that makes things interesting is MakeGenericType(), which
allows you to pass an array of System.Type objects that represent the types that are to be used
in the argument parameter list for the resultant constructed type. For example, creating a
parsing engine for some sort of XML-based language that defines new types from generics is
a snap. Let??™s take a look at an example of how you use the MakeGenericType method:
Imports System
Imports System.Collections.Generic
Public Class EntryPoint
Shared Sub Main()
Dim intList As IList(Of Integer) = _
CType(CreateClosedType(Of Integer)(GetType(List(Of ))), _
IList(Of Integer))
Dim doubleList As IList(Of Double) = _
CType(CreateClosedType(Of Double)(GetType(List(Of ))), _
IList(Of Double))
Console.WriteLine(intList)
Console.WriteLine(doubleList)
End Sub
Private Shared Function CreateClosedType(Of T)(ByVal genericType As Type) _
As Object
Dim typeArguments As Type() = {GetType(T)}
Dim closedType As Type = genericType.MakeGenericType(typeArguments)
Return Activator.CreateInstance(closedType)
End Function
End Class
Here??™s the output from the previous example:
System.
Pages:
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434