|
| 1 | +## About |
| 2 | + |
| 3 | +<!-- A description of the package and where one can find more documentation --> |
| 4 | + |
| 5 | +`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. |
| 6 | + |
| 7 | +Provides `ContainerConfiguration` and some extension methods for the Managed Extensibility Framework (MEF). |
| 8 | + |
| 9 | +## Key Features |
| 10 | + |
| 11 | +<!-- The key features of this package --> |
| 12 | + |
| 13 | +* Provides container configuration. |
| 14 | + |
| 15 | +## How to Use |
| 16 | + |
| 17 | +<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package --> |
| 18 | + |
| 19 | +Register parts from an entire assembly. |
| 20 | + |
| 21 | +```csharp |
| 22 | +using System.Composition; |
| 23 | +using System.Composition.Hosting; |
| 24 | +using System.Reflection; |
| 25 | + |
| 26 | +// Register all parts from the current assembly |
| 27 | +var configuration = new ContainerConfiguration() |
| 28 | + .WithAssembly(Assembly.GetExecutingAssembly()); |
| 29 | + |
| 30 | +using CompositionHost container = configuration.CreateContainer(); |
| 31 | + |
| 32 | +var handlers = container.GetExports<IHandler>(); |
| 33 | +foreach (var handler in handlers) |
| 34 | +{ |
| 35 | + handler.Handle(); |
| 36 | +} |
| 37 | +// HandlerA is handling. |
| 38 | +// HandlerB is handling. |
| 39 | +
|
| 40 | +public interface IHandler |
| 41 | +{ |
| 42 | + void Handle(); |
| 43 | +} |
| 44 | + |
| 45 | +[Export(typeof(IHandler))] |
| 46 | +public class HandlerA : IHandler |
| 47 | +{ |
| 48 | + public void Handle() => Console.WriteLine("HandlerA is handling."); |
| 49 | +} |
| 50 | + |
| 51 | +[Export(typeof(IHandler))] |
| 52 | +public class HandlerB : IHandler |
| 53 | +{ |
| 54 | + public void Handle() => Console.WriteLine("HandlerB is handling."); |
| 55 | +} |
| 56 | +``` |
| 57 | + |
| 58 | +## Main Types |
| 59 | + |
| 60 | +<!-- The main types provided in this library --> |
| 61 | + |
| 62 | +The main types provided by this library are: |
| 63 | + |
| 64 | +* `System.Composition.Hosting.ContainerConfiguration` |
| 65 | +* `System.Composition.CompositionContextExtensions` |
| 66 | + |
| 67 | +## Additional Documentation |
| 68 | + |
| 69 | +<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> |
| 70 | + |
| 71 | +* [Managed Extensibility Framework (MEF)](https://learn.microsoft.com/dotnet/framework/mef/) |
| 72 | + |
| 73 | +## Related Packages |
| 74 | + |
| 75 | +<!-- The related packages associated with this package --> |
| 76 | + |
| 77 | +* [System.Composition](https://www.nuget.org/packages/System.Composition) |
| 78 | +* [System.Composition.AttributedModel](https://www.nuget.org/packages/System.Composition.AttributedModel) |
| 79 | +* [System.Composition.Convention](https://www.nuget.org/packages/System.Composition.Convention) |
| 80 | +* [System.Composition.Hosting](https://www.nuget.org/packages/System.Composition.Hosting) |
| 81 | +* [System.Composition.Runtime](https://www.nuget.org/packages/System.Composition.Runtime) |
| 82 | + |
| 83 | +## Feedback & Contributing |
| 84 | + |
| 85 | +<!-- How to provide feedback on this package and contribute to it --> |
| 86 | + |
| 87 | +System.Composition.TypedParts is released as open source under the [MIT license](https://licenses.nuget.org/MIT). |
| 88 | +Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime). |
0 commit comments