Auf Englisch lesen Bearbeiten

Freigeben über


CompositionContainer.Compose(CompositionBatch) Method

Definition

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

C#
public void Compose(System.ComponentModel.Composition.Hosting.CompositionBatch batch);

Parameters

batch
CompositionBatch

Changes to the CompositionContainer to include during the composition.

Examples

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.

C#
[Export]
class Part1
{
    public String data = "This is the example data!";
}

[Export]
class Part2
{
    [Import]
    public Part1 data { get; set; }
}

[Export]
class Part3
{
    [Import]
    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()));
        container.Compose(batch);
        Part3 _part = container.GetExportedValue<Part3>();
        Console.WriteLine(_part.data.data.data);
        Console.ReadLine();
    }
}

Remarks

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

Produkt Versionen
.NET 8 (package-provided), 9 (package-provided), 10 (package-provided)
.NET Framework 4.0, 4.5, 4.5.1, 4.5.2, 4.6, 4.6.1, 4.6.2, 4.7, 4.7.1, 4.7.2, 4.8, 4.8.1
.NET Standard 2.0 (package-provided)

See also