Created
March 7, 2012 08:09
-
-
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.
This file contains hidden or 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 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