我正在尝试创建一个通用的表达式构建器,只要没有对象值为null,它基本上可以正常工作。
我当前的代码如下所示(以StartsWith为例):
case FilterOperationTypes.StartsWith:
{
ParameterExpression e = Expression.Parameter(typeof(T), "e");
PropertyInfo propertyInfo = typeof(T).GetProperty(field);
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(val, typeof(string));
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.Call(m, mi, c);
return Expression.Lambda<Func<T, bool>>(call, e);
}
我们假设field是Property CustomerName。我知道实际的最终表达式将是:
e.CustomerName.StartsWith(val)
如果CustomerName为null,它当然无法调用StartsWith方法,这是非常清楚的。
我试过这样的事情:
case FilterOperationTypes.StartsWith:
{
ParameterExpression e = Expression.Parameter(typeof(T), "e");
PropertyInfo propertyInfo = typeof(T).GetProperty(field);
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(val, typeof(string));
MethodInfo mi = typeof(string).GetMethod("StartsWith", new Type[] { typeof(string) });
Expression call = Expression.IfThenElse(
Expression.Equal(m, Expression.Constant(null)),
Expression.Constant(null),
Expression.Call(m, mi, c));
//Expression.Call(m, mi, c);
return Expression.Lambda<Func<T, bool>>(call, e);
}
但是这会产生一个类型为'System.Void'的表达式,不能用于返回类型'System.Boolean'Exception。
我现在有点迷失了。也许你们可以把我推向正确的方向。
非常感谢!
答案 0 :(得分:0)
您正在寻找代表条件运算符的Expression.Condition
而不是IfThenElse
,而不是if / else语句。条件运算符解析为一个值,因为它是一个表达式,而不是一个语句。
答案 1 :(得分:0)
现在就开始工作了。非常感谢Servy让我朝着正确的方向前进。
Expression.Condition正是我实现目标所需要的。由于实际对象和对象的属性在运行时可以为null,因此我必须嵌套两个条件:
PropertyInfo propertyInfo = typeof(T).GetProperty(fieldName);
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(comparisonValue, typeof(string));
MethodInfo mi = typeof(string).GetMethod(methodName, new Type[] { typeof(string) });
Expression call = Expression.Condition(Expression.Equal(e, Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)),
Expression.Condition(Expression.Equal(Expression.Property(e, fieldName), Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)), Expression.Call(m, mi, c)));
return Expression.Lambda<Func<T, bool>>(call, e);
如果是空值,我决定使用
Expression.NotEqual(e, Expression.Constant(null))
一定要收到&#34; false&#34;。我确信有更好的方法可以做到这一点,但我想不出一个,但是。
最终的表达式将如下所示
{e => IIF((e == null), (e != null), IIF((e.CustomerName== null), (e != null), e.CustomerName.StartsWith("547")))}
截至目前,我对该解决方案非常满意,但我会尝试优化它。
我当前的代码实现和使用情况如下所示。也许它可以帮助你们中的一个人:
Expression<Func<T, bool>> GetDetailEx<T>(string fieldName, object comparisonValue, string filterType)
{
var e = Expression.Parameter(typeof(T),"e");
switch (GetFilterOperationType(filterType))
{
case FilterOperationTypes.Between:
//Between is automatically translated in >= AND <= within the ExecuteDeepFilter-Method
break;
case FilterOperationTypes.GreaterThanOrEquals:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.GreaterThanOrEqual(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)),e);
}
case FilterOperationTypes.LessThanOrEquals:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.LessThanOrEqual(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)),e);
}
case FilterOperationTypes.GreaterThan:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.GreaterThan(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)), e);
}
case FilterOperationTypes.LessThan:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.LessThan(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)), e);
}
case FilterOperationTypes.NotEqual:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.NotEqual(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)), e);
}
case FilterOperationTypes.Equal:
{
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.Equal(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)), e);
}
case FilterOperationTypes.EndsWith:
{
return GenerateGenericCallExpression<T>(fieldName, comparisonValue, "EndsWith",e);
}
case FilterOperationTypes.StartsWith:
{
return GenerateGenericCallExpression<T>(fieldName, comparisonValue, "StartsWith",e);
}
case FilterOperationTypes.NotContains:
{
return GenerateGenericCallExpression<T>(fieldName, comparisonValue, "Contains",e,false); ;
}
case FilterOperationTypes.Contains:
{
return GenerateGenericCallExpression<T>(fieldName, comparisonValue, "Contains",e);
}
default:
break;
}
return GenerateConditionalExpression<T>(fieldName, comparisonValue, Expression.Equal(
Expression.Convert(Expression.Property(e, fieldName), Expression.Constant(comparisonValue).Type),
Expression.Constant(comparisonValue)), e);
}
private Expression<Func<T, bool>> GenerateConditionalExpression<T>(string fieldName, object comparisonValue, Expression actualExpression, ParameterExpression e)
{
Expression call = Expression.Condition(Expression.Equal(e, Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)),
Expression.Condition(Expression.Equal(Expression.Property(e, fieldName), Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)), actualExpression));
return Expression.Lambda<Func<T, bool>>(call, e);
}
private Expression<Func<T, bool>> GenerateGenericCallExpression<T>(string fieldName, object comparisonValue, string methodName, ParameterExpression e, bool equals = true)
{
PropertyInfo propertyInfo = typeof(T).GetProperty(fieldName);
MemberExpression m = Expression.MakeMemberAccess(e, propertyInfo);
ConstantExpression c = Expression.Constant(comparisonValue, typeof(string));
MethodInfo mi = typeof(string).GetMethod(methodName, new Type[] { typeof(string) });
if (equals)
{
Expression call = Expression.Condition(Expression.Equal(e, Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)),
Expression.Condition(Expression.Equal(Expression.Property(e, fieldName), Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)), Expression.Call(m, mi, c)));
return Expression.Lambda<Func<T, bool>>(call, e);
}
else
{
Expression call = Expression.Condition(Expression.Equal(e, Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)),
Expression.Condition(Expression.Equal(Expression.Property(e, fieldName), Expression.Constant(null)),
Expression.NotEqual(e, Expression.Constant(null)), Expression.Not(Expression.Call(m, mi, c))));
return Expression.Lambda<Func<T, bool>>(call, e);
}
}