我正在尝试创建一个基于lambda函数返回IEqualityComparer的扩展方法。继承人延伸方法:
<Extension()>
Public Function Comparer(Of T)(Func As Func(Of T, T, Boolean)) As IEqualityComparer(Of T)
Return New GenericComparer(Of T)(Func)
End Function
以下是我正在寻找的用法,
Dim CICF = (Function(a As String, b As String) As Boolean
If a.ToUpper = b.ToUpper Then
Return True
Else
Return False
End If
End Function).Comparer
编译器报告错误
'Comparer' is not a member of '<anonymous method>'
如果我将函数分配给显式类型变量,它就可以工作,如下所示:
Dim CICF As Func(Of String, String, Boolean) = (Function(a As String, b As String) As Boolean
If a.ToUpper = b.ToUpper Then
Return True
Else
Return False
End If
End Function)
Dim CIC = CICF.Comparer
所以我的问题是,我能否以一种让我使用我正在寻找的单行风格的方式输入扩展方法?这就是说,如何键入扩展方法来接受匿名方法?
答案 0 :(得分:2)