Skip to content

Instantly share code, notes, and snippets.

@glennblock
Created July 15, 2010 07:29
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save glennblock/476626 to your computer and use it in GitHub Desktop.
Save glennblock/476626 to your computer and use it in GitHub Desktop.
//child catalog is a filtered catalog that contains only parts that should appear in the child (including TPart)
private TPart ComposePartWith<TPart>(ComposablePartCatalog childCatalog, CompositionContainer parentContainer, out IDisposable disposable, params object[] imports)
{
var childContainer = new CompositionContainer(childCatalog, parentContainer);
childContainer.ComposeParts(imports);
disposable = childContainer;
return childContainer.GetExportedValue<TPart>();
}
@glennblock
Copy link
Author

This method creates a child container and populates it with a set of exported instances. It uses a child catalog to set the parts that are in the child container. Finally it retrieves TPart from the child container which should result in the imports that were explicitly passed in being injected. The newly created container is returned as an out param in order to allow it to be disposed. You shoudl always dispoe of child containers to ensure all resources are reclaimed.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment