《C反射表达式树模糊搜索.doc》由会员分享,可在线阅读,更多相关《C反射表达式树模糊搜索.doc(2页珍藏版)》请在三一文库上搜索。
1、C# 反射 表达式树 模糊搜索public反射实体T,非datetime字段反射获取表达式树 static Expressionbool>> GetSearchExpression(string SearchString)Expressionbool>> filter = null;if (string.IsNullOrEmpty(SearchString) return null;var left = Expression.Parameter(typeof(T), "m");Expression expression = Expression.Co
2、nstant(false);T obj = default(T);var type = typeof(T);obj = (T)Activator.CreateInstance(type);var propertyInfos = type.GetProperties(); foreach (var propertyInfo in propertyInfos) if (propertyInfo.Name.ToLower() = "id" | propertyInfo.PropertyType = typeof(DateTime) continue;Expression tost
3、ring = Expression.Call(Expression.Property(left, typeof(T).GetProperty(propertyInfo.Name).Name),typeof(object).GetMethod("ToString", new Type ) );Expression right = Expression.Call(tostring, typeof(string).GetMethod("Contains", new Type typeof(string) ), Expression.Constant(SearchString) );expression = Expression.Or(right, expression);filter = Expression.Lambdabool>>(expression, new left );return filter;