2
void ITest<TSource, TDestination>.TestMethod<TValue>(Expression<Func<TDestination, TValue>> destination, Func<TValue> value)
        {
            // i want to create Func<TValue> to Expression<Func<TSource, TValue>>
        }

I am having method which will be takes argument Func.

I want to convert Func to Expression>

Hemant Malpote
  • 891
  • 13
  • 28
  • Hope this helps http://stackoverflow.com/questions/767733/converting-a-net-funct-to-a-net-expressionfunct – Amit Jan 21 '15 at 07:17

1 Answers1

1

You can't "convert" Func<> to Expression<Func<>>. Func is basically compiled Expression, so only solution is to decompile it, but it is not easily doable.

What you can do is to wrap Func<> with Expression, but I am not sure if it give you anything.

Example:

public static Expression<Func<T>> Wrap<T>(Func<T> f)
{
    return Expression.Lambda<Func<T>>(
        Expression.Invoke(Expression.Constant(f))
    );
}
public static Expression<Func<T1, T2>> Wrap<T1, T2>(Func<T1, T2> f)
{
    var p1 = Expression.Parameter(typeof (T1));
    return Expression.Lambda<Func<T1, T2>>(
        Expression.Invoke(Expression.Constant(f), p1), 
        new[] { p1 }
    );
}
Krzysztof
  • 15,900
  • 2
  • 46
  • 76