2010-08-09 2 views
1

나는이 기능을 구현하려면 :Expression에서 람다를 추출 하시겠습니까?

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

UPDATE를

나는 다음과 같은 문제는,이 함수의 부분이 있습니다

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

답변

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

글쎄, 내가 틀린 대답을 썼다면, 제발 도와주세요, 제 질문을 업데이트했습니다. – Shimmy

+0

@Shimmy - 예외는 'Func'의'표현식 '을'Func '처럼 사용하려고한다는 것입니다. 'Func'의'Expresson'에서'Func'을 얻는 방법은이 답안에서 볼 수 있듯이'Compile' 메소드를 호출하는 것입니다. –

+0

내 문제는 LambdaExpression에 명시 적으로 캐스팅 한 후에 컴파일을 찾지 못했습니다. – Shimmy