Dim TestObject As TestObjectType.GetType
將在命名空間TestObjectType
中尋找名爲GetType
的類型。
若要使用System.Type
一個類的實例,你可以使用Activator.CreateInstance
:
Dim TestObject = Activator.CreateInstance(TestObjectType.GetType())
要創建一個通用的列表,你可以使用Type.MakeGenericType
:
Dim listType = GetType(List(Of)).MakeGenericType(TestObjectType.GetType())
Dim list = Activator.CreateInstance(listType)
注意,這兩個以上片段返回Object
;不過,你可以利用泛型來實現編譯時的安全性:
Dim TestObject = CreateNew(TestObjectType)
Dim AuthorList = CreateNewList(TestObjectType)
...
Function CreateNew(Of T As New)(obj As T) As T
Return New T()
End Function
Function CreateNewList(Of T)(obj As T) As List(Of T)
Return New List(Of T)
End Function
我懷疑這會編譯 – Renaissance