程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> c#反射表達式樹范糊搜刮示例

c#反射表達式樹范糊搜刮示例

編輯:C#入門知識

c#反射表達式樹范糊搜刮示例。本站提示廣大學習愛好者:(c#反射表達式樹范糊搜刮示例)文章只能為提供參考,不一定能成為您想要的結果。以下是c#反射表達式樹范糊搜刮示例正文



public static Expression<Func<T, bool>> GetSearchExpression<T>(string SearchString)
        {
            Expression<Func<T, bool>> filter = null;

            if (string.IsNullOrEmpty(SearchString)) return null;
            var left = Expression.Parameter(typeof(T), "m");

            Expression expression = Expression.Constant(false);
            T obj = default(T);
            var type = typeof(T);
            obj = (T)Activator.CreateInstance(type);
           var propertyInfos = type.GetProperties();

            foreach (var propertyInfo in propertyInfos)
            {

                if (propertyInfo.Name.ToLower() == "id" || propertyInfo.PropertyType == typeof(DateTime)) continue;
                Expression tostring = Expression.Call
         (
            Expression.Property(left, typeof(T).GetProperty(propertyInfo.Name).Name),

           typeof(object).GetMethod("ToString", new Type[] { })

         );
                Expression right = Expression.Call

                      (

                          tostring,

                        typeof(string).GetMethod("Contains", new Type[] { typeof(string) }),

                        Expression.Constant(SearchString)

                      );
                expression = Expression.Or(right, expression);
            }

            filter = Expression.Lambda<Func<T, bool>>(expression, new[] { left });

            return filter;

        }

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved