vb.net – 匿名函数的扩展方法

我正在尝试创建一个基于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

所以我的问题是,我可以用一种方式输入扩展方法,让我使用我正在寻找的单行样式吗?这就是说,我如何键入扩展方法来接受匿名方法?

最佳答案 坦率地说,我不认为这是可能的.

点赞