What is the difference between ExpressionFuncT, object>> and ExpressionFuncobject, object>>?

c# expression-trees lambda linq linq-expressions

Question

Is it possible to convert?Expression<Func<T, object>> to Expression<Func<object, object>> ?

1
2
11/18/2011 9:23:56 PM

Popular Answer

How about the following?

static Expression<Func<object,object>> ConvertFunction<T>(Expression<Func<T,object>> function)      
{
    ParameterExpression p=Expression.Parameter(typeof(object));

    return Expression.Lambda<Func<object,object>>
    (
        Expression.Invoke(function,Expression.Convert(p,typeof(T))), p
    );
}

Then, you may say anything along these lines:

Expression<Func<string,object>> foo=s=>s.Length;
Expression<Func<object,object>> bar=ConvertFunction(foo);

var call=bar.Compile();
Console.Write(call("hello")) ; // Prints 5
1
11/23/2016 11:43:34 AM


Related Questions





Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow