【发布时间】:2013-12-11 22:40:54
【问题描述】:
此代码正确返回一行:
_loadedAssemblies.ForEach(x =>
{
foundTypes.AddRange(from t in x.GetTypes()
where t.GetInterfaces().Contains(typeof(TInterface))
&& t.BaseType.Name.LeftOf('`') == baseClass.Name.LeftOf('`')
select t);
}
但是,当我使用 PredicateBuilder 时,我得到零行:
var compiledPredicate = CompiledPredicate<TInterface>();
_loadedAssemblies.ForEach(x =>
{
foundTypes.AddRange(from t in x.GetTypes()
where compiledPredicate.Invoke(typeof(TInterface))
select t);
}
private static Func<Type, bool> CompiledPredicate<T>() where T : class
{
// True means all records will be returned if no other predicates are applied.
var predicate = PredicateBuilder.True<Type>();
// Get types that implement the interface (T).
predicate = predicate.And(t => t.GetInterfaces().Contains(typeof(T)));
// If the config file includes filtering by base class, then filter by it.
if (!string.IsNullOrWhiteSpace(_baseClass))
{
Type baseClass = Type.GetType(_baseClass);
predicate = predicate.And(t => t.BaseType.Name.LeftOf('`') == baseClass.Name.LeftOf('`'));
}
return predicate.Compile();
}
Someone suggested 创建我的循环变量的副本,但我尝试了,但仍然得到零行。我不确定为什么使用 PredicateBuilder 不会返回任何行。知道我缺少什么吗?
【问题讨论】:
-
如果您使用 linq to objects,为什么还要使用
PredicateBuilder?它真的只对处理表达式有用。 -
@Servy 因为我不知道更好。使用 linq 来对象是这里的问题吗?
-
在使用 linq to objects 时有更简单的方法。你在用大锤敲钉子。
-
好的,很高兴知道。那么有什么更好的方法呢?
-
我不知道为什么,但这样做很有效:
foundTypes.AddRange(x.GetTypes().AsQueryable().Where(compiledPredicate));
标签: c# predicatebuilder