Removing part from container

Jul 18, 2009 at 1:10 AM
Edited Jul 18, 2009 at 1:16 AM
// Form1.cs
// --
public partial class Form1 : Form, IPartImportsSatisfiedNotification {
[Import(typeof(CompositionContainer))] CompositionContainer _container = null;
[ImportMany(typeof(IDependency), AllowRecomposition=true)] Lazy<IDependency>[] _dependencies = null;
void UseDependencies() { ... }
void RemoveADependencyFromContainer {
this._container.ReleaseExport(_dependencies[0]); // doesn't remove dependency from container!!! } void IPartImportsSatisfiedNotification.OnImportsSatisfied() { ... }
} [Export(typeof(IDependency))] class Dependency : IDependency { ... } // Program.cs // -- class Program { CompositionContainer _container = null; Form1 _appForm = null; static void Main(string[] args) { new Program().Run(); } void Run() { this._appForm = new Form1(); this._container = new CompositionContainer(new AssemblyCatalog(Assembly.GetExecutingAssembly())); CompositionBatch batch = new CompositionBatch(); batch.AddPart(this._appForm); batch.AddExportedValue(this._container); this.m_container.Compose(batch); Application.Run(this._appForm); } }

Let's say that at one point in the application, I want to remove a specific part from the container; how would I go about it? ReleaseExport method does not remove it from the container i.e. the OnImportsSatisfied method is not called after ReleaseExport as I expect it to. Am I missing something here? What am I doing wrong?

 

Jul 18, 2009 at 6:14 AM
Edited Jul 18, 2009 at 6:16 AM

Nick is on vacation and is not available now, please leave your name, number and a brief description and he will get back to you when he returns. :-)

Jul 18, 2009 at 10:05 AM

gomikochar, CompositionBatch has a RemovePart method that you can use to remove parts that were added to the container.

Glenn