开发者

Extension Method for Anonymous Function

I'm trying to create an extension method that returns an IEqualityComparer based on a lambda function. Heres the extension method:

<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

Here is the usage I'm looking for,

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

The compiler reports the error

'Comparer' is not a member of '<anonymous method>'

If I assign the function to an explicitly typed variable it works, like this:

        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

So my questions is, can I type the extension method in a way that lets me use the one-liner style I'm looking for? Thats to say, how can I type the extension method to accept an anonymous method?


Frankly, I don't think that is possible.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜