在声明时为Func指定一个参数,在调用/执行时指定另一个参数

时间:2016-04-22 05:02:52

标签: .net vb.net generics delegates func

根据我的研究,我没有找到任何证据证明这是可能的,所以我想知道是否有办法做到这一点或下一个最干净的解决方案?

我希望避免将另一个参数传递给我的泛型函数,以保持其清洁并提高模块性。

考虑以下通用循环函数,该函数在循环集合时调用谓词函数来检查某个条件:

Private Sub LoopAndApplyCondition(Of T)(ByVal collection As IDataCollection, ByVal condition As Func(Of T, String, Boolean), ByRef result As List(Of T))
  If Not collection.ActiveItems Is Nothing And collection.Count > 0 Then
    For Each record In collection
   '***
   '*** I would like to pass in the record into the predicate function here ***
   '***
        Dim meetsCondition As Boolean = condition.Invoke(CType(record, T))
        If meetsCondition Then result.Add(CType(record, T))
    Next
  End If
End Sub

这是定义谓词函数(条件)并调用这个泛型循环函数,它有attributeName字段,这是我想传递给谓词函数的。

Public Function AllEditableRecords(Of T)(ByVal collection As IDataObjectCollection, ByVal attributeName As String) As List(Of T)
    Dim result As New List(Of T)
    '***
    '*** I would like to pass in the attributeName field to the predicate function here ***
    '***
    Dim condition As Func(Of T, String, Boolean) = AddressOf CheckIfRecordIsEditable
    LoopAndApplyCondition(Of T)(collection, condition, result)
  Return result
End Function

这是谓词函数的签名:

Private Function CheckIfRecordIsEditable(Of T)(record As T, attributeName As String) As Boolean
  'Returns conditionResult
End Function

总而言之,我想通过 AllEditableRecords 函数将字符串参数传递给 CheckIfRecordIsEditable ,并通过传递记录参数传递的 LoopAndApplyCondition

我认为不可能,但请证明我错了。 我很高兴在C#中接受答案,但VB.NET更喜欢。

1 个答案:

答案 0 :(得分:0)

不,在声明代理时无法定义代理的参数。

但是,可能的是将Func及其参数封装在自己的类中:

Public Class RecordCondition(Of T)
    Public Property CheckConditionHandler As Func(Of T, String, Boolean)
    Public Property AttributeName As String
End Class

AllEditableRecords 中创建RecordCondition

Public Function AllEditableRecords(Of T)(ByVal collection As IDataObjectCollection, ByVal attributeName As String) As List(Of T)
    Dim result As New List(Of T)

    Dim recordCondition As New RecordCondition(Of T) With {.CheckConditionHandler = AddressOf CheckIfRecordIsEditable, .AttributeName=attributeName}
    LoopAndApplyCondition(Of T)(collection, recordCondition, result)
    Return result
End Function

LoopAndApplyCondition

中致电CheckConditionHandler
Private Sub LoopAndApplyCondition(Of T)(ByVal collection As IDataCollection, ByVal condition As RecordCondition(Of T), ByRef result As List(Of T))
    If Not collection.ActiveItems Is Nothing And collection.Count > 0 Then
        For Each record In collection
            Dim meetsCondition As Boolean = condition.CheckConditionHandler(record, condition.AttributeName) 
            If meetsCondition Then result.Add(CType(record, T))
        Next
    End If
End Sub

CheckIfRecordIsEditable 不会更改。