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. (初学JS)JS基础——ATM机终端程序编写<1.0>

    初步学习了JS基础,为了更好地将所学知识熟练运用,我进行了银行ATM存取款机的模拟程序编写,主要通过VScode终端实现系列操作. 我的ATM程序包括6个主要功能:1.查询余额 2.存钱 3. 取钱 ...

  2. Potplay视频播放画面扭曲

    Potplayer是一款非常好用的视频播放器,解码快,功耗低同时相对较好支持保真加速,但是,在使用过程中出现了如下的所谓“Bug” 经过摸索发现是播放器自动检测到此视频是360视频(不明觉厉,貌似需要 ...

  3. hash模式与history模式

    随着 ajax 的使用越来越广泛,前端的页面逻辑开始变得越来越复杂,特别是spa的兴起,前端路由系统随之开始流行. 从用户的角度看,前端路由主要实现了两个功能(使用ajax更新页面状态的情况下): 记 ...

  4. 设计模式-05建造者模式(Builder Pattern)

    1.模式动机 比如我们要组装一台电脑,都知道电脑是由 CPU.主板.内存.硬盘.显卡.机箱.显示器.键盘和鼠标组成,其中非常重要的一点就是这些硬件都是可以灵活选择,但是组装步骤都是大同小异(可以组一个 ...

  5. postgresql spi开发笔记

    #include "postgres.h" #include "fmgr.h" #include <string.h> #ifdef PG_MODU ...

  6. selenium 操作下拉处理

    操作下拉框处理 在网页中,有时候会遇到下拉框处理,这时候使用Webdriver提供的select类来处理. ##操作下拉框处理 #coding = utf-8 from selenium import ...

  7. 研究微信红包分配算法之Golang版

    今天来看一下红包的分配,参考几年前流传的微信红包分配算法,今天用Golang实现一版,并测试验证结果. 微信红包的随机算法是怎样实现的?https://www.zhihu.com/question/2 ...

  8. Linux中Hadoop的安装与配置

    一.准备 1,配通网络 ping www.baidu.com 之前安装虚拟机时配过 2,关闭防火墙 systemctl stop firewalld systemctl disable firewal ...

  9. C++ 类的赋值运算符'='重载

    什么类需要重载赋值运算符 先来看一个普通类的直接赋值. #include <iostream> using namespace std; class person{ int age; pu ...

  10. Hapi+MySql项目实战自动化文档生成(四)

    自动化生成swagger文档 使用hapi插件hapi-swagger,简单配置下插件,先修改下plugin_config.js文件: //plugin_config.js const Swagger ...