Created
June 6, 2012 09:44
-
-
Save codewithpassion/2880981 to your computer and use it in GitHub Desktop.
#MEF ImportMany on interfaces
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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