错误消息是“无法找到类型或命名空间名称'T'。”
???
public static Expression<Func<T, bool>> MakeFilter(string prop, object val)
{
ParameterExpression pe = Expression.Parameter(typeof(T), "p");
PropertyInfo pi = typeof(T).GetProperty(prop);
MemberExpression me = Expression.MakeMemberAccess(pe, pi);
ConstantExpression ce = Expression.Constant(val);
BinaryExpression be = Expression.Equal(me, ce);
return Expression.Lambda<Func<T, bool>>(be, pe);
}
相关链接:
答案 0 :(得分:10)
您需要使方法本身通用:
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val)
-+-
^
+- this
答案 1 :(得分:3)
没有为您的方法定义泛型参数。您应该定义一个(MakeFilter<T>
):
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val)
{
ParameterExpression pe = Expression.Parameter(typeof(T), "p");
PropertyInfo pi = typeof(T).GetProperty(prop);
MemberExpression me = Expression.MakeMemberAccess(pe, pi);
ConstantExpression ce = Expression.Constant(val);
BinaryExpression be = Expression.Equal(me, ce);
return Expression.Lambda<Func<T, bool>>(be, pe);
}
答案 2 :(得分:2)
该方法需要声明为通用(MakeFilter<T>
):
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val)
否则,调用者还能如何指定T
是什么?