This is achievable by using container.GetExportedValues
, this will expose the the classes and methods. They can then be grouped up and composed into the preferred structure. You don't actually need the [ImportMany]
at all. This feels a little backwards, given MEF is meant to be performing the composition for you, but it works in this case.
class Program
{
static void Main(string[] args)
{
Program p = new Program();
p.Run();
Console.Read();
}
void Run()
{
AssemblyCatalog catalog = new AssemblyCatalog(System.Reflection.Assembly.GetExecutingAssembly());
CompositionContainer container = new CompositionContainer(catalog);
Dictionary<IClass, List<Func<int>>> dictionary = new Dictionary<IClass, List<Func<int>>>();
IEnumerable<IClass> classes = container.GetExportedValues<IClass>();
IEnumerable<Func<int>> methods = container.GetExportedValues<Func<int>>();
foreach(var c in classes)
{
dictionary[c] = new List<Func<int>>();
}
foreach(var m in methods)
{
dictionary[(IClass)m.Target].Add(m);
}
foreach(var kvp in dictionary)
{
kvp.Value.ForEach(m => Console.WriteLine(m()));
}
}
}
If you need the metadata use container.GetExports
.