Simple MEF Example

using System;
using System.ComponentModel.

Composition; // This is the assembly
using System.ComponentModel.Composition.Hosting;
using System.Reflection;

namespace SimpleMEF
{
public class Class1
{
static void Main()
{
AssemblyCatalog catalog = new AssemblyCatalog(Assembly.GetExecutingAssembly());
CompositionContainer container = new CompositionContainer(catalog);

User user = new User();
container.ComposeParts(user);
user.Start();
Console.ReadLine();
}
}

class User
{
[Import]
private IExecute execute;

public void Start()
{
Console.WriteLine(execute.Execute());
}
}

[InheritedExport]
public interface IExecute
{
string Execute();
}

class First : IExecute
{

public string Execute()
{
return “First”;
}
}
}

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s