views:

41

answers:

1

I want to implement this function:

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

UPDATE

I have the following issue, it's a part of a 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

Exception Details:

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  A: 
Public Function GetFunc(Of TSource, TResult) _
    selector As Expression(Of Func(Of TSource, TResult)) _
        As Func(Of TSource, TResult)
    Return selector.Compile()
End Function
Thomas Levesque
Well, I guess I wrote the wrong answer, can you please help me, I updated my question.
Shimmy
@Shimmy - your exception says you're trying to use an `Expression` of a `Func` as if it was a `Func`. The way to get a `Func` from an `Expresson` of a `Func` is to call the `Compile` method, as shown in this answer.
Daniel Earwicker
My problem was I didn't manage to find the Compile, after explicity casting to LambdaExpression I found it.
Shimmy
Thanks for the help, view the complete thing: http://blogs.microsoft.co.il/blogs/shimmy/archive/2010/08/09/how-would-you-check-car-finish-style-year-model-vendor-contacts-firstordefault-fullname-for-null.aspx
Shimmy