Skip to content

Instantly share code, notes, and snippets.

@codewithpassion
Created June 6, 2012 09:44
Show Gist options
  • Save codewithpassion/2880981 to your computer and use it in GitHub Desktop.
Save codewithpassion/2880981 to your computer and use it in GitHub Desktop.
#MEF ImportMany on interfaces
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
var cat = new AssemblyCatalog(typeof(Program).Assembly);
var container = new CompositionContainer(cat);
var someObject = container.GetExportedValue<SomeClass>();
someObject.Init();
Console.ReadLine();
}
}
[Export]
public class SomeClass
{
private readonly IModule[] _modules;
[ImportingConstructor]
public SomeClass([ImportMany(typeof(IModule))] IModule[] modules)
{
_modules = modules;
}
public void Init()
{
_modules.ToList().ForEach(m => Console.WriteLine(m.Name));
}
}
[InheritedExport]
public interface IModule
{
string Name { get; }
}
public class ModuleA : IModule
{
public string Name
{
get { return "ModuleA"; }
}
}
public class ModuleB : IModule
{
public string Name
{
get { return "ModuleB"; }
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment