Search code examples
c#.netmef

Creating multiple instances of Imported MEF parts


Currently my WPF application imports a part like this

[Import(typeof(ILedPanel)]
public ILedPanel Panel { get; set; }

But this gives ma a single intance of the class that implements ILedPanel. What I really want to do is have the ability to create as many instances that I need. Please note there is only one Export for ILedPanel included with the software at any given time.

(If I use an import with List that gives me one instance for every class implementing ILedPanel)

Any suggestions?


Solution

  • There isn't "built in" support for this in MEF today, but before reverting to Service Locator, you might find some inspiration here: http://blogs.msdn.com/nblumhardt/archive/2008/12/27/container-managed-application-design-prelude-where-does-the-container-belong.aspx

    The essential idea is that you 'import' the container into the component that needs to do dynamic instantiation.

    More direct support for this scenario is something we're exploring.

    Nick

    UPDATE: MEF now has experimental support for this. See this blog post for more information.