- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的设置
我有一个存储组件集合的实体类:
public class Entity
{
public Dictionary<Type, IComponent> Components { get; set; }
}
组件是任何实现 IComponent 的类,例如 Position、Orientation 等。
我有一个 Filter 类,它存储条件的集合,这些条件基本上只是谓词:
public class Filter
{
public Dictionary<Type, MulticastDelegate> Conditions;
public Filter()
{
Conditions = new Dictionary<Type, MulticastDelegate>();
}
public void Add<T>(Predicate<T> predicate) where T : class, IComponent
{
Conditions.Add(typeof(T), predicate);
}
}
我的问题
我想要做的是允许创建一个过滤器,该过滤器对不同的组件具有不同的条件(谓词)。我想编写一个方法,为任何其组件都符合条件的实体返回 true。
到目前为止我有什么
public bool IsMatchFor(Entity entity)
{
foreach (IComponent component in entity.Components.Values)
{
if (Conditions.ContainsKey(component.GetType()))
// There is a both a Condition in the Filter and a Component in the Entity that match up based on Type.
// Now check to see if the Condition is satisfied by the Component
{
Type genericPredicate = typeof(Predicate<>);
Type[] typeArgs = { component.GetType() };
Type constructedGenericPredicate = genericPredicate.MakeGenericType(typeArgs);
Predicate<IComponent> predicateInstance = (Predicate<IComponent>)Activator.CreateInstance(constructedGenericPredicate, new object[] { component, Conditions[component.GetType()].Method });
if (!predicateInstance.Invoke(component))
{
return false;
}
}
}
return true;
}
我得到的错误
但是我得到一个错误。基本上我似乎无法构建必要的谓词。有更简单的方法吗?
Test method Tests.Utilities.FilterTests.IsMatchFor_WithASingleConditionThatMatchesEntity_ReturnsTrue threw exception:
System.MissingMethodException: Constructor on type 'System.Predicate`1[[Tests.TestSupportClasses.ComponentType1, Tests, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null]]' not found.
Result StackTrace:
at System.RuntimeType.CreateInstanceImpl(BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes, StackCrawlMark& stackMark)
at System.Activator.CreateInstance(Type type, BindingFlags bindingAttr, Binder binder, Object[] args, CultureInfo culture, Object[] activationAttributes)
at System.Activator.CreateInstance(Type type, Object[] args)
at Entropy.Utilities.Filter.IsMatchFor(Entity entity) in c:\git\Entropy\Source\Utilities\Filter.cs:line 54
at Tests.Utilities.FilterTests.IsMatchFor_WithASingleConditionThatMatchesEntity_ReturnsTrue() in c:\git\Entropy\Tests\Utilities\FilterTests.cs:line 128
一个用例来说明为什么这比看起来更复杂
我希望能够编写以下代码:
_filter.Add(typeof(ComponentType1), (Predicate<IComponent>)(new Predicate<ComponentType1>(c => c.BoolProperty)));
Assert.IsTrue(_filter.IsMatchFor(_entities[0]));
基本上,通过使用通用谓词,我可以让编辑器帮助我创建条件。然而,上面的行将在编译时失败,因为 Predicate(Of IComponent) 不是从 Predicate(Of ComponentType1) 继承的,即使 ComponentType1 实现了 IComponent。
最佳答案
看起来您真正需要的是 Dictionary<Type, Predicate<IComponent>>
而不是持有MulticastDelegate
的一般人:
public Dictionary<Type, Predicate<IComponent>> Conditions;
当以这种方式声明时,您根本不需要使用反射,只需检索 Predicate<IComponent>
即可。从字典中很容易地找到:
public bool IsMatchFor(Entity entity)
{
foreach (IComponent component in entity.Components.Values)
{
Predicate<IComponent> componentPredicate;
if (Conditions.TryGetValue(component.GetType(), out componentPredicate))
{
return componentPredicate(component);
}
}
return false;
}
关于c# - 如何调用通用谓词的集合?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27211908/
我正在开发一个包含 TreeViewer 的 RCP 应用程序,我想在其上激活多个应与“OR”谓词配合使用的过滤器,例如: A |--B |--|--redColor |--|--blueColor
我的问题是关于 enable_if通常标准库中的谓词,但我将在迭代器类型的上下文中构建它,因为这是我目前遇到此问题的地方。 我有一个自定义迭代器类型 It , 这样 std::iterator_tra
关闭。这个问题不满足Stack Overflow guidelines .它目前不接受答案。 想改善这个问题吗?更新问题,使其成为 on-topic对于堆栈溢出。 4年前关闭。 Improve thi
我需要使用自然数为 2 的幂创建一个 Prolog 谓词。 自然数是:0、s(0)、s(s(0)) 等等。 例如: ?- pow2(s(0),P). P = s(s(0)); false. ?- po
我正在尝试创建一个 NSPredicate 来查找在特定日期范围内包含“ session ”的“项目”。我一开始尝试过这个: [NSPredicate predicateWithFormat:@"AN
很难说出这里问的是什么。这个问题是含糊的、模糊的、不完整的、过于宽泛的或修辞性的,无法以目前的形式得到合理的回答。如需帮助澄清此问题以便重新打开它,visit the help center 。 已关
在JOOQ中,我可以编写如下SQL这样的代码吗? 我不知道如何编写具有多个字段的in谓词。 select some_value from t1 where (t1.id1, t1.id2) i
我正在用 PROLOG 编写一个数独求解器。我希望求解器能够处理所有可能大小的数独,因此我自然需要构造采用可变数量参数的谓词。 (例如在数独中构建“ block ”。) 如何构造或模拟具有可变数量参数
我有一个包含一些 id 的列表,我希望我的 ebean 查询排除这些 id。所以基本上我需要在 ebean 中使用“not in”谓词,就像 sql 一样,但遗憾的是找不到。还有其他方法可以实现这一目
我正在构建一个通用接口(interface)以从类中公开选定的字符串属性,然后我想在每个字段中搜索文本,以检查它是否匹配。 这是我的 IFieldExposer接口(interface): using
我将 Spring Boot 与 Spring JPA 和 Specification Executor 结合使用。我的规范/谓词组合成功地在我的类中搜索了简单的属性。但是,我在搜索其中的对象时遇到了
如果下面的 last_name 为 NULL,它会跳过该列的 WHERE 比较以提高性能吗? AND (last_name IS NULL OR sp.last_name LIKE CONCAT('%
出于好奇:如果我有一个接受多个参数(通常为 1 或 2)并返回 3 个值中的 1 个(而不是 bool 值 true 或 false)的类运算符(或函数等),它是否仍应被调用谓词?还是模糊逻辑的特例?
是否可以创建一个采用装箱值类型并返回该值类型是否等于该类型默认值的方法? 所以我想创建一个具有以下签名的方法: bool IsDefault(object boxedValueType); 注意:当
let selectedConsoles = ["Xbox", "Playstation 4"] let players = realm.objects(Person).filter("console
我正在尝试根据用户搜索文本过滤来自核心数据的结果,但效果很好。我正在努力做到有几个关键术语可以返回特定结果。 我有一个Colour 实体,它与另一个实体ProjectColour 具有对多 关系。 P
std::vector lines; typedef std::vector::iterator iterator_t; iterator_t eventLine = std::find_if(lin
我想在一个列表中找到一个元素的索引,该列表匹配某个谓词,有没有比以下更好的方法: var index = list.IndexOf(list.Find(predicate)); ? 最佳答案 你在找
我正在使用缺少 findall 的高阶 Prolog 变体. 还有一个关于实现我们自己的问题 findall这里:Getting list of solutions in Prolog . 低效的实现
我正在使用 Breeze 过滤客户端请求的数据。我的代码看起来有点像这样: 客户端 - 创建过滤谓词 var predicates = []; var criteriaPredicate = null
我是一名优秀的程序员,十分优秀!