手机
当前位置:查字典教程网 >编程开发 >C#教程 >c#反射表达式树模糊搜索示例
c#反射表达式树模糊搜索示例
摘要:复制代码代码如下:publicstaticExpressionGetSearchExpression(stringSearchString)...

复制代码 代码如下:

public static Expression<Func<T, bool>> GetSearchExpression<T>(string SearchString)

{

Expression<Func<T, bool>> filter = null;

if (string.IsNullOrEmpty(SearchString)) return null;

var left = Expression.Parameter(typeof(T), "m");

Expression expression = Expression.Constant(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 tostring = 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.Lambda<Func<T, bool>>(expression, new[] { left });

return filter;

}

【c#反射表达式树模糊搜索示例】相关文章:

c# 匿名方法的小例子

C#制作鹰眼的详细全过程(带注释)实例代码

C#中使用反射获取结构体实例及思路

解析C#中如何把控件的边框角画为圆弧

c#(Socket)异步套接字代码示例

c#构造初始化的顺序浅析

c#匹配整数和小数的正则表达式

二叉树的遍历算法(详细示例分析)

C#重启远程计算机的代码

c#中返回文章发表的时间差的示例

精品推荐
分类导航