Skip to content

Embed URL

HTTPS clone URL

Subversion checkout URL

You can clone with
or
.
Download ZIP
using System;
using System.Collections.Generic;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.Linq;
using System.Text;
namespace MefDemo
{
class Program
{
static void Main(string[] args)
{
var container = new CompositionContainer(
new AssemblyCatalog(typeof(Program).Assembly)
);
var receiver = container.GetExport<TaskListener>();
receiver.Value.Accept();
receiver.Value.Accept();
receiver.Value.Accept();
Console.ReadKey();
}
}
[Export]
public class TaskListener
{
private readonly ExportFactory<TaskProcessor> taskProcessorFactory;
[ImportingConstructor]
public TaskListener(ExportFactory<TaskProcessor> taskProcessorFactory)
{
this.taskProcessorFactory = taskProcessorFactory;
}
public void Accept()
{
using (var processor = taskProcessorFactory.CreateExport())
{
processor.Value.Process();
}
}
}
[Export]
public class TaskProcessor
{
private readonly DataContext dataContext;
[ImportingConstructor]
public TaskProcessor(DataContext dataContext)
{
Console.WriteLine("Ceated");
this.dataContext = dataContext;
}
public void Process()
{
Console.WriteLine("Processing...");
}
}
[Export]
public class DataContext : IDisposable
{
public DataContext()
{
Console.WriteLine("Data context created");
}
public void Dispose()
{
Console.WriteLine("Data context disposed");
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Something went wrong with that request. Please try again.