using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace MefProject
{
class Program
{
[Export] private string OriginalMessage = "My Message";
[Import] private string Message;
static void Main(string[] args)
{
var p = new Program();
p.Compose();
}
private void Compose()
{
var catalog = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
// Export: OriginalMessage >> Import: Message
Console.WriteLine(Message);
}
}
}
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
namespace SeparatingExports
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("Composing MEF from another class");
var loader = new Loader();
loader.GetCount();
}
}
public class Loader
{
[Import] public MefExport MefExportClass;
public Loader()
{
var catalog = new AssemblyCatalog(typeof(MefExport).Assembly);
var container = new CompositionContainer(catalog);
container.ComposeParts(this);
}
public void GetCount()
{
Console.WriteLine(MefExportClass.MyCount);
Console.WriteLine(MefExportClass.UpdateCount());
}
}
[Export]
public class MefExport
{
public int MyCount { get; set; }
public MefExport()
{
MyCount = 2;
}
public int UpdateCount()
{
MyCount++;
return MyCount;
}
}
}