|
| 1 | +## About |
| 2 | + |
| 3 | +<!-- A description of the package and where one can find more documentation --> |
| 4 | + |
| 5 | +`System.Composition.Runtime` 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 | +This package enables the discovery and composition of parts in applications using MEF 2.0. |
| 8 | +It offers the runtime implementation needed for managing composable parts, resolving dependencies, and dynamically wiring components together. |
| 9 | + |
| 10 | +## Key Features |
| 11 | + |
| 12 | +<!-- The key features of this package --> |
| 13 | + |
| 14 | +* Facilitates runtime discovery and composition of parts. |
| 15 | + |
| 16 | +## How to Use |
| 17 | + |
| 18 | +<!-- A compelling example on how to use this package with code, as well as any specific guidelines for when to use the package --> |
| 19 | + |
| 20 | +Resolve dependencies on the fly and can be useful for dynamically loaded components or plugins. |
| 21 | + |
| 22 | +```csharp |
| 23 | +using System.Composition; |
| 24 | +using System.Composition.Hosting; |
| 25 | + |
| 26 | +var configuration = new ContainerConfiguration() |
| 27 | + .WithPart<Service>(); |
| 28 | + |
| 29 | +using CompositionHost container = configuration.CreateContainer(); |
| 30 | + |
| 31 | +var consumer = new Consumer(container); |
| 32 | +consumer.Execute(); |
| 33 | +// Service is running. |
| 34 | +
|
| 35 | +public interface IService |
| 36 | +{ |
| 37 | + void Run(); |
| 38 | +} |
| 39 | + |
| 40 | +[Export(typeof(IService))] |
| 41 | +public class Service : IService |
| 42 | +{ |
| 43 | + public void Run() => Console.WriteLine("Service is running."); |
| 44 | +} |
| 45 | + |
| 46 | +public class Consumer(CompositionContext context) |
| 47 | +{ |
| 48 | + public void Execute() |
| 49 | + { |
| 50 | + // Use the context to resolve the service |
| 51 | + var service = context.GetExport<IService>(); |
| 52 | + service.Run(); |
| 53 | + } |
| 54 | +} |
| 55 | +``` |
| 56 | + |
| 57 | +## Main Types |
| 58 | + |
| 59 | +<!-- The main types provided in this library --> |
| 60 | + |
| 61 | +The main type provided by this library is: |
| 62 | + |
| 63 | +* `System.Composition.CompositionContext` |
| 64 | + |
| 65 | +## Additional Documentation |
| 66 | + |
| 67 | +<!-- Links to further documentation. Remove conceptual documentation if not available for the library. --> |
| 68 | + |
| 69 | +* [API documentation](https://learn.microsoft.com/dotnet/api/system.composition.compositioncontext) |
| 70 | +* [Managed Extensibility Framework (MEF)](https://learn.microsoft.com/dotnet/framework/mef/) |
| 71 | + |
| 72 | +## Related Packages |
| 73 | + |
| 74 | +<!-- The related packages associated with this package --> |
| 75 | + |
| 76 | +* [System.Composition](https://www.nuget.org/packages/System.Composition) |
| 77 | +* [System.Composition.AttributedModel](https://www.nuget.org/packages/System.Composition.AttributedModel) |
| 78 | +* [System.Composition.Convention](https://www.nuget.org/packages/System.Composition.Convention) |
| 79 | +* [System.Composition.Hosting](https://www.nuget.org/packages/System.Composition.Hosting) |
| 80 | +* [System.Composition.TypedParts](https://www.nuget.org/packages/System.Composition.TypedParts) |
| 81 | + |
| 82 | +## Feedback & Contributing |
| 83 | + |
| 84 | +<!-- How to provide feedback on this package and contribute to it --> |
| 85 | + |
| 86 | +System.Composition.Runtime is released as open source under the [MIT license](https://licenses.nuget.org/MIT). |
| 87 | +Bug reports and contributions are welcome at [the GitHub repository](https://github.com/dotnet/runtime). |
0 commit comments