MEF - POC
MEF - POC for Dependency Injection
interface ILogger
{
void Write(string messgae);
}
class hostMEFApplication
{
[ImportMany]
protected IEnumerable<ILogger> Logger { get; set; }
public void run()
{
Compose();
foreach(var log in Logger)
log.Write("Hello World from MEF");
}
private void Compose()
{
var catalog = new AssemblyCatalog(GetType().Assembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
[Export(typeof(ILogger))]
class ConsoleLogger : ILogger
{
public void Write(string message)
{
Console.WriteLine("ConsoleLogger says" + message);
}
}
[Export(typeof(ILogger))]
class SomeOtherLogger : ILogger
{
public void Write(string message)
{
Console.WriteLine("SomeOtherLogger says" + message);
}
}
class Program
{
static void Main(string[] args)
{
var host = new hostMEFApplication();
host.run();
Console.ReadLine();
}
}
interface ILogger
{
void Write(string messgae);
}
class hostMEFApplication
{
[ImportMany]
protected IEnumerable<ILogger> Logger { get; set; }
public void run()
{
Compose();
foreach(var log in Logger)
log.Write("Hello World from MEF");
}
private void Compose()
{
var catalog = new AssemblyCatalog(GetType().Assembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
}
[Export(typeof(ILogger))]
class ConsoleLogger : ILogger
{
public void Write(string message)
{
Console.WriteLine("ConsoleLogger says" + message);
}
}
[Export(typeof(ILogger))]
class SomeOtherLogger : ILogger
{
public void Write(string message)
{
Console.WriteLine("SomeOtherLogger says" + message);
}
}
class Program
{
static void Main(string[] args)
{
var host = new hostMEFApplication();
host.run();
Console.ReadLine();
}
}
Comments
Post a Comment