从表达式中提取 lambda?

发布于 2024-09-13 07:03:53 字数 1107 浏览 2 评论 0原文

我想实现这个功能:

Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    'Implement here
End Function

更新

我有以下问题,它是函数的一部分:

Dim obj As Object = value
For Each exp In expressions
  If obj Is Nothing Then Return [default]
  Select Case exp.NodeType
    Case ExpressionType.Call
      Dim method = DirectCast(exp, MethodCallExpression)
      Dim info = method.Method          

      If method.Object Is Nothing Then
        Dim args = {obj}.Union(method.Arguments.Skip(1))

        'The following line throws the exception, see details bellow
        obj = info.Invoke(Nothing, args.ToArray)
      Else
        obj = info.Invoke(obj, method.Arguments.ToArray)
      End If

异常详细信息:

ArgumentException:

Object of type 'System.Linq.Expressions.Expression`1  
[System.Func`3[System.Char,System.Char,System.Char]]'  
cannot be converted to type 
'System.Func`3[System.Char,System.Char,System.Char]'.

I want to implement this function:

Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    'Implement here
End Function

UPDATE

I have the following issue, it's a part of a function:

Dim obj As Object = value
For Each exp In expressions
  If obj Is Nothing Then Return [default]
  Select Case exp.NodeType
    Case ExpressionType.Call
      Dim method = DirectCast(exp, MethodCallExpression)
      Dim info = method.Method          

      If method.Object Is Nothing Then
        Dim args = {obj}.Union(method.Arguments.Skip(1))

        'The following line throws the exception, see details bellow
        obj = info.Invoke(Nothing, args.ToArray)
      Else
        obj = info.Invoke(obj, method.Arguments.ToArray)
      End If

Exception Details:

ArgumentException:

Object of type 'System.Linq.Expressions.Expression`1  
[System.Func`3[System.Char,System.Char,System.Char]]'  
cannot be converted to type 
'System.Func`3[System.Char,System.Char,System.Char]'.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

千纸鹤 2024-09-20 07:03:53
Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    Return selector.Compile()
End Function
Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    Return selector.Compile()
End Function
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文