List<string> list = new List<string>(); list.Add("EEEEE"); list.Add("FFFF"); entity.Where(a => list.Contains(a.IP)); --------------------------------------------------------------- public static T Where<T>(this T entity, Expression<Func<T, bool>> func) where T : BaseEntity { MethodCallExpression me = (MethodCallExpression)func.Body; MethodInfo methodInfo = me.Method; if (methodInfo != null && methodInfo.Name == "Contains") { ReadOnlyCollection<Expression> Arguments = me.Arguments; if (Arguments != null && Arguments.Count == 1) { MemberExpression mp = (MemberExpression)Arguments[0]; MemberExpression mo = (MemberExpression)me.Object; ConstantExpression ce = (ConstantExpression)mo.Expression; Params<string, ECondition, object[]> param = new Params<string, ECondition, object[]>(); param.Item1 = mp.Member.Name; param.Item2 = ECondition.Like; param.Item3 = new object[] { }; } } return entity;
}
我现在想用表达式解析到List集合中的值,我在debug模式中看到了 变量ce.Value 中List的值,我怎么获取得到转化为List<string> 类型,我使用强制转化报错。 这里是不是需要使用表达式来获取这个值,具体怎么获取,希望高手帮忙解答一下!
反射可以拿到值:
var constantExpression = ((ConstantExpression)mo.Expression); var value = constantExpression.Value; var type = value.GetType().GetField("list"); //List就是需要的值 var list = type.GetValue(value) as List<string>;
不反射可以么???
@博客周: 额...我暂时就知道这个方法...