How do I import multiple plugins / parts using MEF?

I am new to MEF and am trying to use it to create a pluggable system, but I am stuck with the first step.

I am following the article by Andrew Whitechapel . I downloaded his sample code and it works fine (if you remove one of the "export" assemblies - they are mutually exclusive in their example) and reference the MEF assembly).

The sample illustrates the import of one piece. I want to import multiple parts (they are all based on the same interface). So, I change the example code as follows:

[Import]
// OLD - public Interface.ICalculate Calculate { get; set; }
public IEnumerable<Interface.ICalculate> Calculators { get; set; }

// OLD - Console.WriteLine(
// OLD -     String.Format("{0}", Calculate.Circumference(4)));
foreach (Interface.ICalculate calculator in Calculators)
{
    Console.WriteLine(
    String.Format("{0}", calculator.Circumference(4)));
}

      

I also imported System.Collections.Generic for IEnumerable.

Key change is the first. As far as I understand, this will allow me to import parts from multiple assemblies. However, I am getting the following error:

No valid exports were found that match the constraint

      

At this point, I have not even added a few assembly "plugins". Still just one.

For completeness, here's its export definition (which I haven't touched on) in the "plugin" class library:

[Export(typeof(Interface.ICalculate))]
public class Calculate : Interface.ICalculate

      

Any ideas? I'm scratching my head here. I have searched SO and the MEF forums but could find something to enlighten me.

I am using VS 2008 SP1 (no 2010 beta installed) and latest build of System.ComponentModel.Composition (2009.26.8.0).

+2


source to share


1 answer


The MEF 5 preview changed this. Now you need to use ImportManyAttribute instead of ImportAttribute:

[ImportMany]
public IEnumerable<Intertface.ICalculate> Calculators { get; set; }

      



See the announcement for PR5 for details .

+5


source







All Articles