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;
            }
  • 相关阅读:
    SpringSecurity (Spring权限验证)
    Spring mvc Session拦截器
    判断是否登录的拦截器SessionFilter
    Jquery绑定多个BUTTON 点击事件
    jquery ajax提交表单数据的两种方式
    ASP小贴士/ASP Tips
    遍历组合的实现——VB2005
    应用程序生命周期(墓碑机制(程序和页面))【WP7学习札记之十一】
    反应性扩展框架(Reactive Extensions)【WP7学习札记之十六】
    ASP.NET 4【MSDN参考文档方便自己查阅】
  • 原文地址:https://www.cnblogs.com/Simplerjiang/p/12343188.html
Copyright © 2011-2022 走看看