匿名托管DynamicMethods程序集

时间:2015-07-17 12:13:14

标签: c# asp.net-web-api lambda expression-trees

我有以下代码,我需要在对象列表上实现全文搜索。我设法在控制台应用程序中使用它。问题是我在WebAPI应用程序中开始使用它。我收到一条错误消息,指出Object reference not set to an instance of an object.且来源为Anonymously Hosted DynamicMethods Assembly。 似乎Type T的某些属性为null,当访问它时会弹出错误。 任何人都可以告诉我,我的理解是否正确,如果它是正确的,那么你能帮助我找出如何摆脱零属性吗?

public static IEnumerable<T> FullTextSearch<T>(this List<T> list, string searchKey)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(T), "c");
            MethodInfo containsMethod = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
            var publicProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                .Where(p => p.PropertyType == typeof(string));
            Expression orExpressions = null;
            foreach (MethodCallExpression callContainsMethod in 
                               from property in publicProperties
                               let myProperty =Expression.Property(parameter, property.Name)
                               select Expression.Call(myProperty, "Contains", null, Expression.Constant(searchKey)))
            {
                if (orExpressions == null)
                {
                    orExpressions = callContainsMethod;
                }
                else
                {
                    orExpressions = Expression.Or(orExpressions, callContainsMethod);
                }
            }

            IQueryable<T> queryable = list.AsQueryable<T>();
            MethodCallExpression whereCallExpression = Expression.Call(
                typeof(Queryable),
                "Where",
                new Type[] { queryable.ElementType },
                queryable.Expression,
                Expression.Lambda<Func<T, bool>>(orExpressions, new ParameterExpression[] { parameter }));
            var results = queryable.Provider.CreateQuery<T>(whereCallExpression).ToList();
            return results;
        }

1 个答案:

答案 0 :(得分:0)

好的伙计们,我明白了。这是代码,以防有人需要它。

 public static List<T> FullTextSearch<T>(this List<T> list, string searchKey)
        {
            ParameterExpression parameter = Expression.Parameter(typeof(T), "c");
            MethodInfo containsMethod = typeof(string).GetMethod("Contains", new Type[] { typeof(string) });
            var publicProperties = typeof(T).GetProperties(BindingFlags.Public | BindingFlags.Instance | BindingFlags.DeclaredOnly)
                .Where(p => p.PropertyType == typeof(string));
            Expression orExpressions = null;

            foreach (var callContainsMethod in from property in publicProperties
                                                                let myProperty = Expression.Property(parameter, property.Name)
                                                                let myExpression = Expression.Call(myProperty, "Contains", null, Expression.Constant(searchKey))
                                                                let myNullExp = Expression.Call(typeof(string), (typeof(string).GetMethod("IsNullOrEmpty")).Name, null, myProperty)
                                                                let myNotExp = Expression.Not(myNullExp)
                                                                select new { myExpression, myNotExp })
            {
                var andAlso = Expression.AndAlso(callContainsMethod.myNotExp, callContainsMethod.myExpression);
                if (orExpressions == null)
                {
                    orExpressions = andAlso;
                }
                else
                {
                    orExpressions = Expression.Or(orExpressions, andAlso);
                }
            }

            IQueryable<T> queryable = list.AsQueryable<T>();
            MethodCallExpression whereCallExpression = Expression.Call(
                typeof(Queryable),
                "Where",
                new Type[] { queryable.ElementType },
                queryable.Expression,
                Expression.Lambda<Func<T, bool>>(orExpressions, new ParameterExpression[] { parameter }));
            var results = queryable.Provider.CreateQuery<T>(whereCallExpression).ToList();
            return results;
        }

改变的唯一部分是LINQ,我得到两个表达式,其中包含一个用于&#34;包含&#34;和#34; Not(IsNullOrEmpty)&#34;表达式也是如此。