Calling Generic function + lambda
I have this function in vb.net that I converted from C# for a project I'm working on.
Private Function GetAllFactory(Of T)(ByVal ctor As Construct(Of T)) As List(Of T)
'TODO: Data Access stuff
Dim ds As New DataSet()
Dim entities = New List(Of T)()
For Each dataRow As DataRow In ds.Tables(0).Rows
Dim entity As T = ctor(dataRow)
entities.Add(entity)
Next
Return entities
End Function
and the following delegate
Private Delegate Function Construct(Of T)(ByVal dataRow As DataRow) As T
I tried converting the code to call the function from C# to 开发者_如何学Pythonvb.net
Return GetAllFactory(Of MyType)(row >= New MyType(row))
the above line doesn't work. I'm sort of stuck. I haven't used lambda much in C# and even less in vb.net.
MyType constructor:
Public Sub New(ByVal dataRow As DataRow)
.
.
.
End Sub
Any suggestions on how to call the GetAllFactory?
You use the Function
keyword in VB to write a lambda expression:
Return GetAllFactory(Of MyType)(Function(row) New MyType(row))
Note that >=
is a comparison operator while =>
is the lamda operator in C#. VB might give you some unexpected error message for code using =>
as it accepts that as an undocumented alias for the >=
operator.
VB.Net lambda expressions look like this:
Return GetAllFactory(Of MyType)(Function(row) New MyType(row))
精彩评论