Andrew Andrew - 5 months ago 54 Question

Type T is not defined

So I'm trying to create a generic expression which can be used across all my classes.

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

However T is not defined. How can I use T with VB.NET?

Answer Source

You have to declare the type variable. Change your method signature to:

Function Test(Of T)(field As String) As Func(Of T, String)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download