public static bool StringFilter(this object model,string filterStr)
{
if (string.IsNullOrEmpty(filterStr))
{
return false;
} var modelType = model.GetType();
if (modelType.IsClass) //先检查是否为类
{
foreach (var item in modelType.GetRuntimeProperties()) //只获取第一及的属性值
{
try
{
if (item.PropertyType == typeof(Boolean))
{
if (((bool)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(string))
{
var itemstring = (string)item.GetValue(model);
if (!string.IsNullOrEmpty(itemstring))
{
if (itemstring.Contains("深圳市"))
{ }
if (itemstring.Contains(filterStr)) return true;
} }
else if (item.PropertyType == typeof(DateTime))
{
if (((DateTime)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(int))
{
if (((int)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(long))
{
if (((long)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(short))
{
if (((short)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(decimal))
{
if (((decimal)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(double))
{
if (((int)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(float))
{
if (((float)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<int>))
{
if ((((Nullable<int>)item.GetValue(model))).HasValue) if (((Nullable<int>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<long>))
{
if ((((Nullable<long>)item.GetValue(model))).HasValue) if (((Nullable<long>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<short>))
{
if ((((Nullable<short>)item.GetValue(model))).HasValue) if (((Nullable<short>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<decimal>))
{
if ((((Nullable<decimal>)item.GetValue(model))).HasValue) if (((Nullable<decimal>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<float>))
{
if ((((Nullable<float>)item.GetValue(model))).HasValue) if (((Nullable<float>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType == typeof(Nullable<double>))
{
if ((((Nullable<double>)item.GetValue(model))).HasValue) if (((Nullable<double>)item.GetValue(model)).ToString().Contains(filterStr)) return true;
}
else if (item.PropertyType.IsGenericType)
{
var list = item.PropertyType.GetInterface("IEnumerable", false);
if (list != null)
{
var listVal = item.GetValue(model) as IEnumerable<object>;
foreach (var listitem in listVal)
{
if (listitem.StringFilter(filterStr)) return true;
}
}
}
else if (item.PropertyType.IsClass)
{
var subModel = item.GetValue(model);
if (subModel!=null)
{
if (subModel.StringFilter(filterStr)) return true;
}
}
}
catch (Exception ex)
{ }
}
}
else if (modelType.IsGenericType) //检查是否为迭代器
{
var list = modelType.GetInterface("IEnumerable", false);
if (list != null)
{
var listVal = model as IEnumerable<object>;
foreach (var listitem in listVal)
{
if (listitem.StringFilter(filterStr)) return true;
}
}
}
return false;
}

最新文章

  1. 【xcode5的使用】
  2. PHP mkdir()无写权限的问题解决方法
  3. [水煮 ASP.NET Web API2 方法论](3-3)路由默认值
  4. SQL 函数
  5. demo07
  6. java连接mysql批量写入数据
  7. Swift类型检查与转换
  8. How to Change Password Complexity Requirements in Windows XP
  9. Python用format格式化字符串
  10. Android实现获取本机中所有图片
  11. AxonVR:体验有触觉有温度的VR世界
  12. 强密码和弱密码并没有什么区别?NIST密码安全标准更新:不再建议密码要求混合大写字母、字符和数字
  13. html5本地存储数据,实现自动登录功能
  14. js 阻止事件执行
  15. 快速排序的java实现
  16. Python_装饰器习题_31
  17. synchronized同步方法《二》
  18. SDP服务搜索流程源码分析
  19. vue+ElementUI使用笔记
  20. xlrd、xlwt

热门文章

  1. 01--java--语言概述
  2. 浅谈openresty
  3. (笔记)常用Llinu命令(一)
  4. C语言博客作业8
  5. sys model 常见用法
  6. SVN : 在SVN检测下来的Maven项目没有Maven标志
  7. K8S提示Init:ImagePullBackOff
  8. Visual Studio 2012 出现关于ActivityLog.xml错误的解决方案
  9. thinkphp3关闭Runtime中的日志方法
  10. WeChall_Prime Factory (Training, Math)