简体   繁体   中英

How to specify which DLLs to load with System.Composition (MEF2)?

I'm developing a new plugin loader application framework and reading on all available variants of MEF, I think the most future-proof and flexible is the MEF2 implementation available from NuGet as System.Composition .

Unfortunately, most if not all tutorials online are written for MEF1, ie System.ComponentModel.Composition and use an AssemblyCatalog which is created from eg explicitly finding DLLs in a directory and passing that to the MEF CompositionContainer .

MEF2 does not have this object and thus it is still unclear to me where MEF2 finds its components. Can they not be located at runtime (which would imply each plugin needs to be added as a reference to the application loading the plugins)? That would strike me as odd.

Can anyone shed some light on how to specify what assemblies will be loaded from where (and how on Earth I should set this up) in System.Composition ? I understand MEF2 is designed with PCL in mind (I'm writing desktop apps), but if there is only a small setup price to pay for this future portability, I'm all for it. If not, I'll succumb to using System.ComponentModel.Composition which I can figure out.

The catalogs are replaced by ContainerConfiguration:

        var conf = new System.Composition.Hosting.ContainerConfiguration();
        conf.WithAssembly(Assembly.GetExecutingAssembly());
        var container = conf.CreateContainer();

        var hwWriter = new HelloWorldWriter();
        container.SatisfyImports(hwWriter);

        Console.WriteLine(hwWriter.Write);
        Console.ReadLine();

In ContainerConfiguration you will find many more With... methods which replace all the different catalogs in MEF1.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM