【发布时间】:2015-04-28 18:57:24
【问题描述】:
Expression> lambda = Expression.Lambda >(methodCall, allParameters);这里是错误:
“System.ArgumentException”类型的异常发生在... 附加信息:“System.Func`2[Business.Entities.SlateEmployee,System.Boolean]”类型的表达式不能用于返回类型“System.Boolean”
谁能看到我的代码有什么问题。
public class ClearValueAction : ActionRuleBase
{
/// <summary>
/// Virtual method to call during actions
/// </summary>
/// <returns></returns>
public override bool ExecuteAction<T>(T dataObject)
{
PropertyInfo propertyInfo = typeof (T).GetProperty(Left);
if (propertyInfo != null)
{
ReflectionUtils.SetObjectValue(dataObject, null, propertyInfo);
return true;
}
return false;
}
/// <summary>
/// Making an expression to call the ExecuteAction function when this is compiled
/// </summary>
/// <typeparam name="T"></typeparam>
/// <returns></returns>
public override Func<T, bool> CompileRule<T>()
{
//return Expression.Lambda<Func<T, bool>>().Compile();
ParameterExpression allParameters;
var methodCall = CreateLambda<T>("ExecuteAction", "dataObject", out allParameters);
//ERROR IS HERE//////////////////////////////////////////////
Expression<Func<T, bool>> lambda = Expression.Lambda <Func<T, bool>>(methodCall, allParameters);
return lambda.Compile();
}
private LambdaExpression CreateLambda<T>(string methodName, string methodInputParamName, out ParameterExpression allParameters)
{
allParameters = Expression.Parameter(typeof(T), methodInputParamName);
var c = Expression.Constant(methodInputParamName);
MethodInfo methodInfo = typeof(ClearValueAction).GetMethods().First(m => m.Name == methodName && m.IsGenericMethod).MakeGenericMethod(typeof(T));
MethodCallExpression generateCallExpression = GenerateCallExpression(this, methodInfo, allParameters);
return Expression.Lambda(generateCallExpression, allParameters);
}
/// <summary>
/// Generate expression call
/// </summary>
/// <param name="instance">If instance is NULL, then it method will be treated as static method</param>
private static MethodCallExpression GenerateCallExpression(object instance, MethodBase method, ParameterExpression allParameters)
{
var methodInfo = method as MethodInfo;
// it's non static method
if (instance != null)
{
var instanceExpr = Expression.Convert(Expression.Constant(instance), instance.GetType());
return Expression.Call(instanceExpr, methodInfo, allParameters);
}
// it's static method
return Expression.Call(methodInfo, allParameters);
}
private static List<Expression> GenerateParameters(MethodBase method, out ParameterExpression allParameters)
{
allParameters = Expression.Parameter(typeof(object[]), "params");
ParameterInfo[] methodMarameters = method.GetParameters();
List<Expression> parameters = new List<Expression>();
for (int i = 0; i < methodMarameters.Length; i++)
{
var indexExpr = Expression.Constant(i);
var item = Expression.ArrayIndex(allParameters, indexExpr);
var converted = Expression.Convert(item, methodMarameters[i].ParameterType);
parameters.Add(converted);
}
return parameters;
}
}
【问题讨论】:
-
是否存在内部异常?
-
将
var lambda更改为显式类型。var现在可能是Func<T, bool>类型,而不是Expression<Func<T,bool>>。所以:Expression<Func<T,bool>> lambda = ... -
爱达荷州,不,内部异常为空。
-
Martijn,我已将代码更新为 Expression
> 仍然出错。
标签: c# .net reflection lambda expression