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使用一组约定基于实体类的定义来构建模型. 可指定其他配置以补充或替代约定的内容.本系列介绍的配置可应用于面向任何数据存储的模型,以及面向任意关系数据库时可应用的配置. 数据库提供 ...
随机推荐
- 每天玩转3分钟 MyBatis-Plus - 5. 高级查询(三)(条件构造器)
每天玩转3分钟 MyBatis-Plus - 1. 配置环境 每天玩转3分钟 MyBatis-Plus - 2. 普通查询 每天玩转3分钟 MyBatis-Plus - 3. 高级查询(一) 每天玩转 ...
- Java 构造方法总结
Java 构造方法总结 ①方法名和 类名相同 ②在方法名的前面没有返回值类型的声明 ③在方法中不能使用return语句返回一个值 ④在创建对象时,要调用new,如:book b1=new book() ...
- <背包>solution-POJ1742_Coins
POJ1742 - Coins People in Silverland use coins.They have coins of value A1,A2,A3...An Silverland dol ...
- 实验11:EIGRP
实验8-1:EIGRP 基本配置 实验目的通过本实验可以掌握:(1)在路由器上启动EIGRP 路由进程(2)启用参与路由协议的接口,并且通告网络(3)EIGRP 度量值的计算方法(4)可行距离(FD) ...
- ListFragment 使用ListView and 自定义Adapter
在开发过程中经常使用Tabs + ListFragment 作为表现形式. ListFragment 中加入ListView显示方式很容易. [java] view plaincopy package ...
- 每日一练_PAT_B1001
鲁宾逊先生有一只宠物猴,名叫多多.这天,他们两个正沿着乡间小路散步,突然发现路边的告示牌上贴着一张小小的纸条:“欢迎免费品尝我种的花生!——熊字”.鲁宾逊先生和多多都很开心,因为花生正是他们的最爱.在 ...
- CCF_201604-4_游戏
bfs,首先记录危险方格的时间,因为100时间之后,所有方格均不危险,所以最短时间的最大值为300,记录每个坐标每个时间的状态,直接bfs即可. #include<cstdio> #inc ...
- HDU 6562 lovers 2018CCPC吉林H(线段树)
题意: 初始n个空串,m个操作: 1.给[l,r]的所有字符串头尾加一个‘d’,将原字符串x变为dxd 2.求[l,r]所有字符串代表的数字之和mod 1e9+7 思路: 据说是硬核线段树.. 对于线 ...
- CP - OFDM
- 解释为什么不能依赖fail-fast
我的观点fail-fast是什么就不多解释了,应该注意到的是(以ArrayList为例):modCount位于AbstractList中, protected transient int modCou ...