匿名托管动态方法程序集中的对象引用错误

本文关键字:集中 程序集 对象引用 错误 程序 方法 动态 | 更新日期: 2023-09-27 18:03:24

我有以下代码,我需要在对象列表上实现全文搜索。我设法让它在控制台应用程序中工作。问题是当我开始在WebAPI应用程序中使用它时。我得到一个错误信息,说Object reference not set to an instance of an object.,源是Anonymously Hosted DynamicMethods Assembly。似乎Type T的一些属性是空的,当它被访问的错误弹出。有没有人能告诉我我的理解是否正确,如果是正确的,那么你也能帮助我弄清楚如何摆脱空属性?

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

匿名托管动态方法程序集中的对象引用错误

好了,伙计们,我明白了。下面是代码,以备不时之需。

 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,我得到两个表达式,其中包含一个为"contains"和一个为"Not(IsNullOrEmpty)"表达式,也'ed。