C# 通过反射检查属性是否包含特定字符串
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# 通过反射检查属性是否包含特定字符串的更多相关文章
- Linux 查找当前目录下 包含特定字符串 的所有文件
使用 Linux 经常会遇到这种情况:只知道文件中包含某些特定的字符串,但是不知道具体的文件名.需要根据“特定的字符串”反向查找文件. 示例(路径文件如下): ./miracle/luna/a.txt ...
- javascript 之正则表达式匹配不包含特定字符串的字符
如:有如下字符串,想查出不包含min.js的字符串 ['xx.min.js','xx.js','x.js','x.min.js'] 方法一: 使用逻辑非判断, !/min\.js/.test(str ...
- C#的Winform中OpenFileDialog对话框Filter属性设置包含特定字符,使用正则表达式
OpenFileDialog对话框的Filter属性说明: 首先观察Filter属性的组成部分:“Word文件|*.doc ”,前面的“Word文件”成为标签,是一个可读的字符串,可以自定定义,“|* ...
- Linux----快速注释包含特定字符串的行
常常会需要将程序中的输出语句注释,往往手工非常慢,而且容易漏. 今天研究了 linux 的 sed, 真心好用.. 例子: 将 包含 cout 的语句注释,也就是说包含cout 语句前加入字符串 / ...
- pandas过滤包含特定字符串的行
~df.col3.str.contains('u|z')也就是在条件前面加~号,表示not
- ORACLE 检索某列包含特定字符串的数据表工具存储过程
使用示例: delete APPS.FIND_RESULT; set serveroutput ondeclare v_ret varchar(200);begin apps.sp_f ...
- C#利用反射来判断对象是否包含某个属性的实现方法
本文实例展示了C#利用反射来判断对象是否包含某个属性的实现方法,对于C#程序设计人员来说有一定的学习借鉴价值. 具体实现代码如下: /// <summary> /// 利用反射来判断对象是 ...
- [Effective JavaScript 笔记]第29条:避免使用非标准的栈检查属性
许多js环境都提供检查调用栈的功能.调用栈是指当前正在执行的活动函数链.在某些旧的宿主环境中,每个arguments对象含有两个额外的属性:arguments.callee和arguments.cal ...
- asp.net core系列 23 EF模型配置(概述, 类型和属性的包含与排除)
一.模型配置概述 EF使用一组约定基于实体类的定义来构建模型. 可指定其他配置以补充或替代约定的内容.本系列介绍的配置可应用于面向任何数据存储的模型,以及面向任意关系数据库时可应用的配置. 数据库提供 ...
随机推荐
- STM32学习笔记:基础例子
本例子代码参考了STM32库开发实战指南中的代码,由于使用的板子是尚学STM32F103ZET6,为了配合板上已有资源,也参考了其配套代码.为了便于书写文本,我尽量将代码都写到了一个文件中,这种方式是 ...
- 红黑树(依照4阶B树C++实现)
我在编写红黑树的时候类比这2-3-4树的原理来书写 语言标准:C++11 在Ubuntu 18.04上通过编译和测试 从刚开始只听说过这个概念,到学习,再到编出代码,然后在进行测试,最后完成代码一共花 ...
- Nutz-使用Jspview跳转页面报404
案例 今天在前段页面提交登陆请求时,后台报404,找不到对应的页面,但是该jsp已经放在了正确的目录下,并且请求地址也没错,就是返回对应jsp的时候找不到该文件 解决方案 经排查,原来犯了个低级错误, ...
- Brokers类型配置
模块 配置项 作用域 备注 DynamicConnectionQuota max.connectionsmax.connections.per.ipmax.connections.per.ip.ove ...
- node + multer存储element-ui上传的图片
说明 element-ui的Upload组件可以帮助我们上传我们的图片到我们的服务器,可以使用action参数上传图片,也可以使用http-request自定义上传方式.这里我们使用自定义的方式上传. ...
- iNeuOS工业互联平台,iNeuKernel(物联网核心组件)远程控制标准化设计与实现。发布v2.3版本。
目 录 1. 概述... 2 2. 平台演示... 2 3. 控制端与iNeuKernel的交互协议... 3 4. 设备驱动实现控制业务... 4 ...
- inode和block的理解
什么是inode和block? 所谓的inode就是索引节点(index node)的意思,在每一个存储设备被格式化创建文件系统后,所有的文件大致被分为了两部分,分别是inode和block. 其中i ...
- Windows API 教程(九) 网络编程
茵蒂克丝 基础概念 ip 地址 服务端与客户端 Socket 基础概念 头文件和库文件 常用函数 WSAStartup ( ) 函数 WSACleanup ( ) 函数 Socket ( ) 函数 c ...
- C++ STL IO流 与 Unicode (UTF-16 UTF-8) 的协同工作
09年研究技术的大神真的好多,本文测试有很多错误,更正后发布下(可能与编辑器相关). file.imbue(locale(file.getloc(), new codecvt_utf8<wcha ...
- 小白学 Python 数据分析(3):Pandas (二)数据结构 Series
在家为国家做贡献太无聊,不如跟我一起学点 Python 顺便问一下,你们都喜欢什么什么样的文章封面图,老用这一张感觉有点丑 人生苦短,我用 Python 前文传送门: 小白学 Python 数据分析( ...