我有以下对象:
public static class ExpressionEntities
{
public static Entity1 alias1 = null;
public static Entity2 alias2 = null;
public static Entity3 alias3 = null;
}
我正在尝试动态构建以下lambda表达式:
() => ExpressionEntities.alias1.someProperty == 0
要动态构建它,我知道可以使用Expression类。我查看了this,但是下面出现了运行时错误。这是我到目前为止的内容:
var arg = Expression.Constant(null, typeof(ExpressionEntites));
var prop = Expression.PropertyOrField(arg, "alias1"); // This line throws a runtime error
prop = Expression.PropertyOfField(prop, "someProperty");
var body = Expression.Equal(prop, Expression.Constant(0));
Expression<Func<bool>> exp = Expression.Lambda<Func<bool>>(body);
这是正在抛出的错误:
System.ArgumentException:'alias1'不是'Namespace.ExpressionEntities'类型的成员
任何人都知道如何使它工作以构建lambda表达式吗?