代码之家  ›  专栏  ›  技术社区  ›  Shimmy Weitzhandler 500 - Internal Server Error

从表达式中提取lambda?

  •  1
  • Shimmy Weitzhandler 500 - Internal Server Error  · 技术社区  · 14 年前

    我想实现这个功能:

    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]'.
    
    1 回复  |  直到 14 年前
        1
  •  2
  •   Thomas Levesque    14 年前
    Public Function GetFunc(Of TSource, TResult) _
        selector As Expression(Of Func(Of TSource, TResult)) _
            As Func(Of TSource, TResult)
        Return selector.Compile()
    End Function