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;
}

C# 通过反射检查属性是否包含特定字符串的更多相关文章

  1. Linux 查找当前目录下 包含特定字符串 的所有文件

    使用 Linux 经常会遇到这种情况:只知道文件中包含某些特定的字符串,但是不知道具体的文件名.需要根据“特定的字符串”反向查找文件. 示例(路径文件如下): ./miracle/luna/a.txt ...

  2. javascript 之正则表达式匹配不包含特定字符串的字符

    如:有如下字符串,想查出不包含min.js的字符串  ['xx.min.js','xx.js','x.js','x.min.js'] 方法一: 使用逻辑非判断, !/min\.js/.test(str ...

  3. C#的Winform中OpenFileDialog对话框Filter属性设置包含特定字符,使用正则表达式

    OpenFileDialog对话框的Filter属性说明: 首先观察Filter属性的组成部分:“Word文件|*.doc ”,前面的“Word文件”成为标签,是一个可读的字符串,可以自定定义,“|* ...

  4. Linux----快速注释包含特定字符串的行

    常常会需要将程序中的输出语句注释,往往手工非常慢,而且容易漏. 今天研究了 linux  的 sed, 真心好用.. 例子: 将 包含 cout 的语句注释,也就是说包含cout 语句前加入字符串 / ...

  5. pandas过滤包含特定字符串的行

    ~df.col3.str.contains('u|z')也就是在条件前面加~号,表示not

  6. ORACLE 检索某列包含特定字符串的数据表工具存储过程

    使用示例: delete APPS.FIND_RESULT; set serveroutput ondeclare     v_ret varchar(200);begin     apps.sp_f ...

  7. C#利用反射来判断对象是否包含某个属性的实现方法

    本文实例展示了C#利用反射来判断对象是否包含某个属性的实现方法,对于C#程序设计人员来说有一定的学习借鉴价值. 具体实现代码如下: /// <summary> /// 利用反射来判断对象是 ...

  8. [Effective JavaScript 笔记]第29条:避免使用非标准的栈检查属性

    许多js环境都提供检查调用栈的功能.调用栈是指当前正在执行的活动函数链.在某些旧的宿主环境中,每个arguments对象含有两个额外的属性:arguments.callee和arguments.cal ...

  9. asp.net core系列 23 EF模型配置(概述, 类型和属性的包含与排除)

    一.模型配置概述 EF使用一组约定基于实体类的定义来构建模型. 可指定其他配置以补充或替代约定的内容.本系列介绍的配置可应用于面向任何数据存储的模型,以及面向任意关系数据库时可应用的配置. 数据库提供 ...

随机推荐

  1. 编译游戏库allegro

    一个allegro依赖了大概十个库,还得自己一个个的去编译,然后复制粘贴 主要从两个网页学到的 第一个网页里有绝大多数的依赖库的编译方法 http://wiki.allegro.cc/index.ph ...

  2. RMQ算法使用ST表实现

    RMQ RMQ (Range Minimum Query),指求区间最小值.普通的求区间最小值的方法是暴力. 对于一个数列: \[ A_1,~ A_2,~ A_3,~ \cdots,~ A_n \] ...

  3. ubuntu “快捷方式”

    1.创建.Desktop文件 sudo gedit /usr/share/applications/pycharm.desktop 2.建立pycharm.desktop [Desktop Entry ...

  4. 什么是LakeHouse?

    1. 引入 在Databricks的过去几年中,我们看到了一种新的数据管理范式,该范式出现在许多客户和案例中:LakeHouse.在这篇文章中,我们将描述这种新范式及其相对于先前方案的优势. 数据仓库 ...

  5. Python3基础之正则表达式

    正则表达式 在线测试工具 http://tool.chinaz.com/regex/ 同一个位置上可以出现的字符的范围. 字符组 : [字符组] 在同一个位置可能出现的各种字符组成了一个字符组,在正则 ...

  6. Redis 中的数据持久化策略(AOF)

    上一篇文章,我们讲的是 Redis 的一种基于内存快照的持久化存储策略 RDB,本质上他就是让 redis fork 出一个子进程遍历我们所有数据库中的字典,进行磁盘文件的写入. 但其实这种方式是有缺 ...

  7. Mondriaan's Dream 轮廓线DP 状压

    Mondriaan's Dream 题目链接 Problem Description Squares and rectangles fascinated the famous Dutch painte ...

  8. CTF--HTTP服务--SQL注入-X-Forwarded-For报文头

    开门见山 1. 扫描靶场ip,发现PCS 192.168.31.196 2. 扫描靶场开放服务信息 3. 扫描靶场全部信息 4. 探测敏感信息 5. 查看靶场80端口的主界面 6. 使用AVWS工具进 ...

  9. Docker扩展内容之容器环境变量

    介绍 docker容器设置环境变量除了可以在容器层面的变量文件中加载也可以在容器运行之初进行预加载环境变量,下面介绍在Dockerfile中编写环境变量的方式 ENV TZ=Asia/Shanghai ...

  10. vue 新建脚手架项目npm命令

    使用国外原镜像 npm install -g @vue/cli   //yarn global add @vue/cli            使用淘宝镜像 cnpm install -g @vue/ ...