using System.Linq.Expressions;
using FastExpressionCompiler;
public static void Main()
public static MethodInfo MethodOf(Expression<Func<object>> func)
var mi = GetMemberInfo(func);
return mi is PropertyInfo ? ((PropertyInfo)mi).GetGetMethod() : (MethodInfo)mi;
public static MemberInfo GetMemberInfo(LambdaExpression func)
if (ex is UnaryExpression)
ex = ((UnaryExpression)ex).Operand;
if (ex.NodeType == ExpressionType.New)
return ((NewExpression)ex).Constructor;
ex is MemberExpression ? ((MemberExpression)ex).Member :
ex is MethodCallExpression ? ((MethodCallExpression)ex).Method : (MemberInfo)((NewExpression)ex).Constructor;
public static TReturn InnerCall<TReturn>(TReturn input)
static MethodInfo _toStringMethod = MethodOf(() => default(object).ToString());
static MethodInfo _innerMethod = MethodOf(() => Program.InnerCall<int>(default(int))).GetGenericMethodDefinition();
public static void TestString()
var param = Expression.Parameter(typeof(Test), "x");
var predicate = Expression.Lambda<Func<Test, Test>>(
Expression.New(typeof(Test)),
Expression.Bind(typeof(Test).GetProperty("Name"),
_innerMethod.MakeGenericMethod(typeof(int)),
Expression.Property(param, "Id")
var fn = predicate.Compile();
var ffn = predicate.CompileFast();
var ffnRes = ffn(entity);
Console.WriteLine("TestString(): OK!");
public int Id { get; set; }
public string Name { get; set; }