Padmanaban
Padmanaban

Reputation: 75

Mef compose parts for all classes present in an assembly

public class ClassMerger
{         

    [Import(typeof(ITest))]
    public ITest pTest { get; set; }

    [Import(typeof(INewTest))]
    public INewTest ObjImportClass { get; set; }

    private AggregateCatalog catalog { get; set; }
    public static CompositionContainer container { get; set; }

    public void Compose()
    {
        DirectoryCatalog dc = new DirectoryCatalog(@" Debug");
        catalog = new AggregateCatalog(dc);
        AssemblyCatalog catalog1 = new AssemblyCatalog(Assembly.GetExecutingAssembly());
        catalog.Catalogs.Add(catalog1);
        container = new CompositionContainer(catalog);             
        container.ComposeParts(catalog);
    }        
}

In the above code when i say container.composeparts(catalog) all my interfaces(e.g., ITest) are null. But when i rewrite it as container.ComposeParts(this) the parts are composed. If so how do i mention my container to compose parts for all the classes which are present in my assembly.

Upvotes: 1

Views: 1633

Answers (1)

You get null because it will never try to compose for your class ClassMerger, but rather for AssemblyCatalog, elaborating:

When you say container.Composeparts(catalog); you are telling MEF to compose parts for decorated properties inside your instance of AssemblyCatalog.

On the other hand, when you tell container.ComposeParts(this); you are telling MEF to compose those decorated properties on the instance of the class ClassMerger, thus finally finding your properties to fill in.

Note that by container = new CompositionContainer(catalog); you are telling MEF where to look for parts that may satisfy your later-to-load, decorated properties when you fire .ComposeParts...

Upvotes: 2

Related Questions