2014-07-22 36 views
0

我知道我可以得到一個類型傳遞的對象類型的這種方式如何獲取對象類型的傳遞給函數匿名類型,而無需創建該對象

Public Function ConvertToValidationDataModel(Of T)(ByVal oSourceObject As Object) As Object 
     Dim oDestinationObject As Object 
     Dim oDestinationObjectType As Type 

     oDestinationObject = Activator.CreateInstance(Of T)() 

     oDestinationObjectType = oDestinationObject.GetType() 

End Function 

但實例是有辦法獲得一個類型而不是創建對象的實例?

其他詞 - 有沒有這樣的事情?

Dim oType AS Type = GetType(Of T) 
+0

不能打電話的GetType(T )? –

+0

當我做'Dim oType As Type = GetType(OF T)'它強調並且說'關鍵字不會命名一個類型' –

+1

再次閱讀我的評論,它說GetType(T)不是GetType(的T) –

回答

1

你有型已經,這是通用的參數T.下面是一個小控制檯應用程序,其輸出一起:

http://grab.by/yO2S

Module Module1 
Sub Main() 
    Foo(Of Integer)(1) 
    Foo(Of String)(1) 

    Console.WriteLine() 
    Console.WriteLine(Foo(Of Boolean)(True)) 

    Console.ReadLine() 
End Sub 

Public Function Foo(Of T)(ByVal oSourceObject As Object) As Type 
    If TypeOf oSourceObject Is T Then 
     Console.WriteLine("Types match.") 
    Else 
     Console.WriteLine("Types mismatch.") 
    End If 

    Return GetType(T) 
End Function 
End Module 
相關問題