请参阅以下代码:
Public Iterator Function Read(Of T)(ByVal sql As String, ByVal make As Func(Of IDataReader, T), ParamArray ByVal parms() As Object) As IEnumerable(Of T)
Using connection = CreateConnection()
Using command = CreateCommand(sql, connection, parms)
Using reader = command.ExecuteReader()
Do While reader.Read()
Yield make(reader) --line 7
Loop
End Using
End Using
End Using
End Function
Private Shared Make As Func(Of IDataReader, Member) =
Function(reader) _
New Member() With {
.MemberId = Extensions.AsId(reader("MemberId")),
.Email = Extensions.AsString(reader("Email")),
.CompanyName = Extensions.AsString(reader("CompanyName")),
.City = Extensions.AsString(reader("City")),
.Country = Extensions.AsString(reader("Country"))
}
请参阅第7行。使用数据读取器行中的值填充Member类型的对象。我已阅读以下文档:http://msdn.microsoft.com/en-us/library/9k7k7cf0.aspx。文档似乎没有解释当您使用委托时发生的情况,即Yield make(datareader),而不是Yield return datareader。控件是否传递回调用函数以及委托(Make)?
答案 0 :(得分:0)
Make(reader)
是Make.Invoke(reader)
的快捷方式。即,你
reader
作为参数传递,产生类型为Member
的值。Yield
返回该值。相当于:
...
Do While reader.Read()
Dim myMember As Member = make(reader)
Yield myMember
Loop
...
PS:如果您的代码中出现编译时错误(在您的问题中没有这样说):这是因为您的方法被声明为返回IEnumerable(Of T)
,事实上,它返回IEnumerable(Of Member)
。