English 中文(简体)
MEF如何确定其进口顺序?
原标题:How does MEF determine the order of its imports?
  • 时间:2009-11-20 13:01:36
  •  标签:
  • c#
  • mef

MEF允许你通过使用<代码>ImportMany属性进口多个部件。 它如何确定收回相关出口的先后次序,并增加多少出口量。 例如,我如何进口不得不按特定顺序发射的多个《国际规则》? 我认为,唯一一种办法是在《规则》中以人工方式拥有传令财产:

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();
    }
  }
}
最佳回答

否则,贸易与贸易部就无法保证进口的任何出口订单。 然而,在货币基金中,你可以通过使用一些元数据和一项习俗收集来做一些订购。 例如,你可以做这样的事情:

public interface IRule { }

[Export(typeof(IRule))]
[ExportMetadata("Order", 1)]
public class Rule1 : IRule { }

[Export(typeof(IRule))]
[ExportMetadata("Order", 2)]
public class Rule2 : IRule { }

public interface IOrderMetadata
{
    [DefaultValue(Int32.MaxValue)]
    int Order { get; }
}

public class Engine
{
    public Engine()
    {
        Rules = new OrderingCollection<IRule, IOrderMetadata>(
                           lazyRule => lazyRule.Metadata.Order);
    }

    [ImportMany]
    public OrderingCollection<IRule, IOrderMetadata> Rules { get; set; }
}

然后,你将制定一套由元数据订购的规则。 可在

问题回答

你们可以相互进口规则(使用决标模式),但每一规则都需要了解预设规则的具体规则,这可能是你想要的。

交通和交通部将帮助你解救这些部件,你与这些部件做的是你们。 如果你想放弃这些部分,那就没有错了!





相关问题
Anyone feel like passing it forward?

I m the only developer in my company, and am getting along well as an autodidact, but I know I m missing out on the education one gets from working with and having code reviewed by more senior devs. ...

NSArray s, Primitive types and Boxing Oh My!

I m pretty new to the Objective-C world and I have a long history with .net/C# so naturally I m inclined to use my C# wits. Now here s the question: I feel really inclined to create some type of ...

C# Marshal / Pinvoke CBitmap?

I cannot figure out how to marshal a C++ CBitmap to a C# Bitmap or Image class. My import looks like this: [DllImport(@"test.dll", CharSet = CharSet.Unicode)] public static extern IntPtr ...

How to Use Ghostscript DLL to convert PDF to PDF/A

How to user GhostScript DLL to convert PDF to PDF/A. I know I kind of have to call the exported function of gsdll32.dll whose name is gsapi_init_with_args, but how do i pass the right arguments? BTW, ...

Linqy no matchy

Maybe it s something I m doing wrong. I m just learning Linq because I m bored. And so far so good. I made a little program and it basically just outputs all matches (foreach) into a label control. ...

热门标签