I'm new to MEF and am trying to use it to build a plug-in system, but am stuck at step one.
I'm following an article by Andrew Whitechapel. I've downloaded his sample code an it runs OK (if you remove one of the "exporting" assemblies - they are mutually exclusive in his sample - and reference the MEF assembly).
The sample illustrates importing a single part. I want to import multiple parts (all based on the same interface). So, I change the sample 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 the IEnumerable.
The key change is the first one. As I understand it, this will allow me to import parts from multiple assemblies. However, I get the following error:
No valid exports were found that match the constraint
At this point I haven't even added multiple "plugin" assemblies. Still just have the one.
For completeness here's his export definition (which I haven't touched) in the "plugin" class library:
[Export(typeof(Interface.ICalculate))]
public class Calculate : Interface.ICalculate
Any ideas? I'm scratching my head here. I've searched SO and the MEF forums, but could find anything to enlighten me.
I'm using VS 2008 SP1 (no 2010 beta installed) and the latest System.ComponentModel.Composition assembly (2009.26.8.0).