匿名托管的 DynamicMethods 程序集中的对象引用错误
Object reference error in Anonymously Hosted DynamicMethods Assembly
我有以下代码,我需要在其中对对象列表实施全文搜索。我设法让它在控制台应用程序中工作。问题是当我开始在 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)" 表达式 andalso'ed .
我有以下代码,我需要在其中对对象列表实施全文搜索。我设法让它在控制台应用程序中工作。问题是当我开始在 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)" 表达式 andalso'ed .