The error message is "The type or namespace name 'T' could not be found."
???
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);
}
Related links:
You need to make the method itself generic:
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val)
-+-
^
+- this
There's no generic argument defined for your method. You should define one ( 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);
}
The method needs to be declared as generic ( MakeFilter<T>
):
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val)
Otherwise, how else would the caller be able to specify what T
is?
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.