动态构建Lambda表达式

 
  
    using System;
using System.Linq.Expressions;

class Program
{
static void Main(string[] args)
{
ParameterExpression parameterExpression = Expression.Parameter(typeof(TestClass));
MemberExpression testPropertyExpression = Expression.Property(parameterExpression, "TestProperty");

Expression<Func<TestClass, string>> expressionLambda = Expression.Lambda<Func<TestClass, string>>(testPropertyExpression, new ParameterExpression[] { parameterExpression });
Func<TestClass, string> dss = expressionLambda.Compile();

TestClass test = new TestClass();
test.TestProperty = "Hello!";
Object a = dss.DynamicInvoke(new[] { test });
}
}

public class TestClass
{
public string TestProperty { get; set; }
}

你可能感兴趣的:(lambda)