CompositionContainer.Compose(CompositionBatch) Method


Adds or removes the parts in the specified CompositionBatch from the container and executes composition.

 void Compose(System::ComponentModel::Composition::Hosting::CompositionBatch ^ batch);
public void Compose (System.ComponentModel.Composition.Hosting.CompositionBatch batch);
member this.Compose : System.ComponentModel.Composition.Hosting.CompositionBatch -> unit
Public Sub Compose (batch As CompositionBatch)



Changes to the CompositionContainer to include during the composition.


In this simple example, three parts are created and added to the CompositionContainer, and one part is retrieved to show that all imports have been filled. This example uses the Attributed Programming Model.

class Part1
    public String data = "This is the example data!";

class Part2
    public Part1 data { get; set; }

class Part3
    public Part2 data { get; set; }

class Program
    static void Main(string[] args)
        CompositionContainer container = new CompositionContainer();
        CompositionBatch batch = new CompositionBatch();
        batch.AddPart(AttributedModelServices.CreatePart(new Part1()));
        batch.AddPart(AttributedModelServices.CreatePart(new Part2()));
        batch.AddPart(AttributedModelServices.CreatePart(new Part3()));
        Part3 _part = container.GetExportedValue<Part3>();
Public Class Part1
    Public ReadOnly Property data As String
            Return "This is the example data!"
        End Get
    End Property
End Class
Public Class Part2
    Public Property data As Part1
End Class

Public Class Part3
    Public Property data As Part2
End Class

Sub Main()
    Dim container As New CompositionContainer()
    Dim batch As New CompositionBatch()
    batch.AddPart(AttributedModelServices.CreatePart(New Part1()))
    batch.AddPart(AttributedModelServices.CreatePart(New Part2()))
    batch.AddPart(AttributedModelServices.CreatePart(New Part3()))
    Dim _part As Part3
    _part = container.GetExportedValue(Of Part3)()

End Sub


This method is the primary way of directly adding or removing parts from the container. The CompositionContainer will always maintain a stable, composed state. Therefore, calling Compose with an empty CompositionBatch is never necessary to start composition. Instead, call the Compose method whenever you need to make changes to the parts available to the CompositionContainer.

The CompositionBatch can contain both parts to be added and parts to be removed. Recomposition will take place only once for each call to Compose.

Applies to