Skip to content

Instantly share code, notes, and snippets.

@chgeuer
Created March 7, 2012 08:09
Show Gist options
  • Save chgeuer/1991842 to your computer and use it in GitHub Desktop.
Save chgeuer/1991842 to your computer and use it in GitHub Desktop.
Demonstrates how a MEF CompositionContainer itself ensures that it can be [Import]ed as well.
namespace ExposeAMefContainer
{
using System;
using System.ComponentModel.Composition;
using System.ComponentModel.Composition.Hosting;
using System.ComponentModel.Composition.Primitives;
class Program
{
static void Main(string[] args)
{
var cc = CompositionContainerFactory.Create(new TypeCatalog(typeof(Bar), typeof(Foo)));
var bar = cc.GetExportedValue<Bar>();
var cc2 = bar.Foo.CC;
if (object.ReferenceEquals(cc, cc2))
{
Console.WriteLine("Success");
}
}
}
[Export]
public class Foo
{
[Import]
public CompositionContainer CC { get; set; }
}
[Export]
public class Bar
{
[Import]
public Foo Foo { get; set; }
}
public static class CompositionContainerFactory
{
public static CompositionContainer Create(ComposablePartCatalog catalog)
{
var result = new CompositionContainer(new AggregateCatalog(catalog,
new TypeCatalog(typeof(CompositionContainer))));
result.ComposeExportedValue<CompositionContainer>(result);
return result;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment