日期:2014-05-18  浏览次数:21441 次

Lambda表达式中如何处理闭包?
我想从一个接受到的lambda表达式中解析出参数的值,例如定义一个表达式如下:
string p ="1" ;
Expression<Func<PLY_BASE, bool>> exp = c =>c.VIP_FLAG==p;


解析的方法:

MyMethod(LambdaExpression exp)
{

var expr = exp.Body as BinaryExpression;
var obj = expr.Right as System.Linq.Expressions.MemberExpression;
var value = (obj.Expression as ConstantExpression).Value;

//这里的value得到的是一个闭包类型,{.DisplayClass}
如何得到其真正的值“1”呢?

}

------解决方案--------------------
C# code

            var expr = exp.Body as BinaryExpression;
            var obj = expr.Right as System.Linq.Expressions.MemberExpression;
            var value = (obj.Expression as ConstantExpression).Value;
            var p = value.GetType().GetField("p").GetValue(value);