MEF部件的添加删除与重排
AddPart/RemovePart
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
class Program
{
static void Main(string[] args)
{
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(catalog);
var root = new Root();
// add external part
container.ComposeParts(root);
// ... use the composed root instance
// removes external part
batch = new CompositionBatch();
batch.RemovePart(root);
container.Compose(batch);
}
}
public class Root
{
[Import(RequiredCreationPolicy = CreationPolicy.NonShared)]
public NonSharedDependency Dep { get; set; }
}
[Export, PartCreationPolicy(CreationPolicy.NonShared)]
public class NonSharedDependency : IDisposable
{
public NonSharedDependency()
{
}
public void Dispose()
{
Console.WriteLine("Disposed");
}
}
Recomposition 重排
public class HttpServerHealthMonitor
{
[ImportMany(AllowRecomposition=true)]
public IMessageSender[] Senders { get; set; }