Извлечь лямбду из выражения? - PullRequest
1 голос
/ 09 августа 2010

Я хочу реализовать эту функцию:

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 Ответ

2 голосов
/ 09 августа 2010
Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    Return selector.Compile()
End Function
...