2017-09-25 103 views
2

所以我試圖創建一個通用表達式,可以用於我的所有類。類型T沒有定義

Function Test(field As String) As Func(Of T, String) 
     Dim xParam = Expression.Parameter(GetType(T), "o") 
     Dim prop = Expression.Property(xParam, GetType(T).GetProperty(field)) 
     Dim lambda = Expression.Lambda(Of Func(Of T, String))(prop, xParam) 
     Return lambda.Compile() 
    End Function 

但是T沒有定義。我如何在VB.NET中使用T?

+2

'Function Test(Of T)(field As String)As Func(Of T,String)' – LarsTech

回答

3

您必須聲明類型變量。改變你的方法簽名爲:

Function Test(Of T)(field As String) As Func(Of T, String) 
+0

Ahh,這很有道理。多麼簡單,謝謝 – Andrew

相關問題