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

原文地址:https://www.cnblogs.com/Simplerjiang/p/12343188.html

时间: 2024-08-29 17:16:38

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

正则表达式不包含特定字符串

概述 做日志分析工作的经常需要跟成千上万的日志条目打交道,为了在庞大的数据量中找到特定模式的数据,常常需要编写很多复杂的正则表达式.例如枚举出日志文件中不包含某个特定字符串的条目,找出不以某个特定字符串打头的条目,等等. 使用否定式前瞻 正则表达式中有前瞻(Lookahead)和后顾(Lookbehind)的概念,这两个术语非常形象的描述了正则引擎的匹配行为.需要注意一点,正则表达式中的前和后和我们一般理解的前后有点不同.一段文本,我们一般习惯把文本开头的方向称作“前面”,文本末尾方向称为“后面

shell判断变量内容里包含特定字符串

shell判断变量内容里包含特定字符串 shell [ "$str" =~ "IEEE80211" ] && echo "it contains IEEE80211" [email protected] 2017-5-11

遍历根目录下包含特定字符串的文件

import os import re name = raw_input("please input the name: ") for dirpath, dirnames, filenames in os.walk(os.path.join('/home/xiao', name), True, None): for filename in filenames: if re.search('test', filename): # if 'test' in filename: print

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

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

[C#]利用反射来判断对象是否包含某个属性

关键代码: /// <summary> /// 利用反射来判断对象是否包含某个属性 /// </summary> /// <param name="instance">object</param> /// <param name="propertyName">需要判断的属性</param> /// <returns>是否包含</returns> public static

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

许多js环境都提供检查调用栈的功能.调用栈是指当前正在执行的活动函数链.在某些旧的宿主环境中,每个arguments对象含有两个额外的属性:arguments.callee和arguments.caller.前者指向使用该arguments对象被调用的函数.后者指向调用该arguments对象被调用的函数的函数.许多环境支持arguments.callee,但它除了允许匿名函数递归地引用自身之外,没有更多的用途了.(高3中认为使用arguments.callee可以解除函数体内的代码和函数名之间

跟王老师学反射(七)使用反射调用属性

跟王老师学反射(七):使用反射调用属性 主讲教师:王少华   QQ群号:483773664 学习内容 使用反射调用属性 通过Class对象的getFields()|getDeclaredFields()或getField()|getDeclaredField()方法可以获取该类所有包括的全部属性或指定的属性 一.Field类用于猎取类中的属性的方法 (一)getXxx(Object obj) 获取obj对象该Field的属性值,此处Xxx对应8个基本类型. (二)setXxx(Object ob

java 反射 根据属性 动态设置值

package com.jhl.jvm.lesson8; import java.lang.reflect.Field; /** * * @author jhl * java 反射 根据属性 动态设置值 demo * */ public class ExceptionLog { private String exceptionLogId; private String processingType; private String type; private String content; pub

linux查找特定类型的文件中是否包含特定字段

shell是个好东西,极大的方便了查询工作,之前遇到一个问题,查询包含有特定字段的特定文件,经过查询,命令如下: 1 find . -type f -name '*.cpp' print|xargs grep -r '#include' -l 上述命令的意思是查询包含有 '#include' 字符的 cpp文件,并给出文件名列表.具体需要熟悉find 和grep命令和各个参数的意思. 参考链接:http://blog.sina.com.cn/s/blog_691a84f301015khx.htm