I have coded a service like that :
public interface IMyInterface
{
...
}
[Export(typeof(IMyInterface))]
internal class MyService : IMyInterface
{
...
}
Now, I’d like to import several instances of MyService with MEF in my main program.
How can I do that ?
With [Import] private IMyInterface MyService { get; set; } I only get 1 instance of MyService.
In my main program, I’d like to dynamically specify the number of imported instance of MyService before MEF composition.
I don’t want to use [ImportMany] because I don’t want to specify the number of export in my MyService implementation.
Can you help me ?
You probably don’t want to do this as straight imports, but get the exported value from the container several times. Because of this, you need to change the creation policy to NonShared, which forces the container to instantiate a new instance each time.
Then get the value from the container: