MEF 允许您通过使用ImportMany
属性导入多个部件。它如何确定检索相关导出并将它们添加到您正在填充的枚举中的顺序?例如,我将如何导入必须以特定顺序触发的多个 IRule?我能想到的唯一方法是在 IRule 中有一个 OrderValue 属性并手动排序:
public class Engine
{
[ImportMany]
public IEnumerable<IRule> Rules { get; set; }
public void Run()
{
// ...
// Initialise MEF
// ...
//
// Do I need to manually order Rules here?
//
foreach (IRule rule in Rules)
{
// Must execute in a specific order
rule.Execute();
}
}
}