MEF: how to manually configure export for contract implementation

I have a MEF CompositionContainer, a contract (say IFoo), and a module (Prism module, but here it doesn't really matter, just some kind of component). I want the contract implementation (FooImpl) to be registered in my module.

If it is Unity, I would do the following:

unity.RegisterType<IFoo, FooImpl>().

It's all. With MEF I'm puzzled. I should mention my implementation of ExportAttribute, but this will lead to its automatic export. I want to deal with this myself. Take a look at the code:

class MyModule: IModule {
  private CompositionContainer m_container;
  public MyModule(CompositionContainer container) {
    m_container = container;
  }
  public void Initialize() {
    ??? I want something like m_container.CreateExport<IFoo, FooImpl>()
  }
}

public interface IFoo {}
public class FooImpl : IFoo {
    //[ImportingConstructor]
    public FooImpl(ISomeService svc) {}
}

Initialize FooImpl IFoo, ExportAttribute FooImpl. , FooImpl ( MyModule.Initialize ), FooImpl /, , .

, , : CompositionContainer ? , ImportingConstructorAttribute?

+3
3

, MEF . ExportProvider, , CompositionContainer.

MEF contrib . , , , . , .

0

AttributedModelServices.ComposeExportedValue. :

m_container.ComposeExportedValue<IFoo>(new IFooImpl());
+6

MEF RegistrationBuilder, MEF . MEF CodePlex.

.

+1
source

Source: https://habr.com/ru/post/1759892/


All Articles