2014-07-22 94 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 
相关问题