2010-08-09 4 views
1

Je veux mettre en œuvre cette fonction:Extrait le lambda d'une expression?

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

MISE À JOUR

J'ai la question suivante, il est une partie d'une fonction:

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 Détails:

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

Répondre

2
Public Function GetFunc(Of TSource, TResult) _ 
    selector As Expression(Of Func(Of TSource, TResult)) _ 
     As Func(Of TSource, TResult) 
    Return selector.Compile() 
End Function 
+0

Eh bien, je suppose que j'ai écrit la mauvaise réponse, pouvez-vous s'il vous plaît m'aider, j'ai mis à jour ma question. – Shimmy

+0

@Shimmy - votre exception dit que vous essayez d'utiliser une 'Expression' d'un' Func' comme s'il s'agissait d'un 'Func'. La façon d'obtenir un 'Func' d'un' Expresson' d'un 'Func' est d'appeler la méthode' Compile', comme montré dans cette réponse. –

+0

Mon problème était que je n'arrivais pas à trouver la Compile, après avoir explicité le casting en LambdaExpression je l'ai trouvé. – Shimmy