动态构建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; }
}
posted @ 2010-06-25 14:30  昝昝  阅读(319)  评论(0)    收藏  举报