Returning a nested generic expression <Func <T, bool >>
Error message: "Type or namespace name" T "not 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:
Using reflection to address the Linqed property
Creating a Runtime Generic Func <T>
+7
Axl
source share3 answers
You need to make this method general:
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val) -+- ^ +- this +10
Lasse VΓ₯gsΓ¦ther Karlsen
source shareThere is no general argument specific to your method. You must 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); } +3
Mehrdad afshari
source shareThis method should be declared as general ( MakeFilter<T> ):
public static Expression<Func<T, bool>> MakeFilter<T>(string prop, object val) Otherwise, how else would the caller indicate that T ?
+2
Rex m
source share