zoukankan      html  css  js  c++  java
  • 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;
            }
  • 相关阅读:
    Mysql初始化root密码和允许远程访问
    windows下nodejs express安装及入门网站,视频资料,开源项目介绍
    python3.4学习笔记(二十六) Python 输出json到文件,让json.dumps输出中文 实例代码
    python3.4学习笔记(二十五) Python 调用mysql redis实例代码
    python3.4学习笔记(二十四) Python pycharm window安装redis MySQL-python相关方法
    python3.4学习笔记(二十三) Python调用淘宝IP库获取IP归属地返回省市运营商实例代码
    python3.4学习笔记(二十二) python 在字符串里面插入指定分割符,将list中的字符转为数字
    python3.4学习笔记(二十一) python实现指定字符串补全空格、前面填充0的方法
    python3.4学习笔记(二十) python strip()函数 去空格 函数的用法
    python3.4学习笔记(十九) 同一台机器同时安装 python2.7 和 python3.4的解决方法
  • 原文地址:https://www.cnblogs.com/Simplerjiang/p/12343188.html
Copyright © 2011-2022 走看看