MEF permits Import (via injection of properties or constructor) along with their own dependencies on exported types in registered registers registered in the directory (including the current assembly).
If you want to create an object directly (using the new keyword), or if the export was not ready at the time of creation, you can use the container to satisfy the import of the object using:
_container.SatisfyImportsOnce(yourObject);
I put together a small script doing just that. here is the code:
public class Demo { private readonly CompositionContainer _container; [Import] public IInterface Dependency { get; set; } public Demo(CompositionContainer container) { _container = container; } public void Test() {
Now just use new Tests(new CompositionContainer()).Test(); to start the demo.
Hope this helps :)
Abdoumoumen
source share