Back to Dotnet

PACKAGE

src/libraries/System.Composition.TypedParts/src/PACKAGE.md

11.0.1002.7 KB
Original Source

About

<!-- A description of the package and where one can find more documentation -->

System.Composition.TypedParts is part of the Managed Extensibility Framework (MEF) 2.0, a composition library for .NET that enables dependency injection through attributes or conventions.

Provides ContainerConfiguration and some extension methods for the Managed Extensibility Framework (MEF).

Key Features

<!-- The key features of this package -->
  • Provides container configuration.

How to Use

<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package -->

Register parts from an entire assembly.

csharp
using System.Composition;
using System.Composition.Hosting;
using System.Reflection;

// Register all parts from the current assembly
var configuration = new ContainerConfiguration()
    .WithAssembly(Assembly.GetExecutingAssembly());

using CompositionHost container = configuration.CreateContainer();

var handlers = container.GetExports<IHandler>();
foreach (var handler in handlers)
{
    handler.Handle();
}
// HandlerA is handling.
// HandlerB is handling.

public interface IHandler
{
    void Handle();
}

[Export(typeof(IHandler))]
public class HandlerA : IHandler
{
    public void Handle() => Console.WriteLine("HandlerA is handling.");
}

[Export(typeof(IHandler))]
public class HandlerB : IHandler
{
    public void Handle() => Console.WriteLine("HandlerB is handling.");
}

Main Types

<!-- The main types provided in this library -->

The main types provided by this library are:

  • System.Composition.Hosting.ContainerConfiguration
  • System.Composition.CompositionContextExtensions

Additional Documentation

<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> <!-- The related packages associated with this package -->

Feedback & Contributing

<!-- How to provide feedback on this package and contribute to it -->

System.Composition.TypedParts is released as open source under the MIT license. Bug reports and contributions are welcome at the GitHub repository.