Vb.net 从表达式中提取lambda?

Vb.net 从表达式中提取lambda?,vb.net,linq,lambda,expression-trees,Vb.net,Linq,Lambda,Expression Trees,我想实现这个功能: 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

我想实现这个功能:

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
更新

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

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]'.

嗯,我想我写错了答案,你能帮帮我吗,我更新了我的问题。@Shimmy-你的例外情况是你试图使用
Func
表达式,就像它是
Func
一样。从
Func
Expresson
获取
Func
的方法是调用
Compile
方法,如此答案所示。我的问题是,在显式转换到LambdaExpression后,我没有找到编译。感谢您的帮助,查看完整内容: