Addpart/removepart
Using system;
Using system. componentmodel. composition;
Using system. componentmodel. Composition. Hosting;
Using system. componentmodel. Composition. primitives;
Class Program
{
Static void main (string [] ARGs)
{
VaR catalog = new assemblycatalog (typeof (Program). Assembly );
VaR Container = new compositioncontainer (Catalog );
VaR root = New Root ();
// Add external part
Container. composeparts (Root );
//... Use the composed root instance
// Removes external part
Batch = new compositionbatch ();
Batch. removepart (Root );
Container. Compose (batch );
}
}
Public class Root
{
[Import (requiredcreationpolicy = creationpolicy. nonshared)]
Public nonshareddependency Dep {Get; set ;}
}
[Export, partcreationpolicy (creationpolicy. nonshared)]
Public class nonshareddependency: idisposable
{
Public nonshareddependency ()
{
}
Public void dispose ()
{
Console. writeline ("disposed ");
}
}
Recomposition Rearrangement
Public class httpserverhealthmonitor
{
[Importmany (allowrecomposition = true)]
Public imessagesender [] senders {Get; set ;}