我在一个类库项目中有一个名为Product
的类。我正在使用SubSonic SimpleRepository
来持久化对象。我在Product
类中有以下方法:
public static IList<Product> Load(Expression<Func<Product, bool>> expression)
{
var rep=RepoHelper.GetRepo("ConStr");
var products = rep.Find(expression);
return products.ToList();
}
我像这样调用了这个函数:
private void BindData()
{
var list = Product.Load(x => x.Active);//Active is of type bool
rptrItems.DataSource = list;
rptrItems.DataBind();
}
在BindData
中调用Load
会抛出异常:
variable 'x' of type 'Product' referenced from scope '', but it is not defined
我该如何解决这个问题。
编辑:通过逐步查看 SubSonic
代码,我发现错误是由这个函数引起的。
private static Expression Evaluate(Expression e)
{
if(e.NodeType == ExpressionType.Constant)
return e;
Type type = e.Type;
if(type.IsValueType)
e = Expression.Convert(e, typeof(object));
Expression<Func<object>> lambda = Expression.Lambda<Func<object>>(e);
Func<object> fn = lambda.Compile(); //THIS THROWS EXCEPTION
return Expression.Constant(fn(), type);
}