Using methods available in plugin1 in plugin2 via MEF

I have a MEF console application that hosts a host ( CompositionContainer ), from which available plug-in assemblies are loaded based on a command line parameter, for example:

 app.exe plugin1 

load host (app.exe) and plugin1. VS solution is structured in such a way that each plugin has its own project (hence its own assembly).

There is a set of plugins, some of which have the ability to reuse code. So, for example, plugin1 has the CopyFiles(string fileName) method and plugin2 should use the same functionality.

Now, in a traditional console application, you add a link to plugin1.dll and, using this namespace, get down to business with reuse.

I'm curious if there is a β€œMEF” way, if you want, to implement this reuse. So, something like creating a plugin1 object in plugin2 , adding the Import attribute to plugin1's export interface and letting MEF take care of the rest. Is it possible? And if so, how?

Or is there a better approach to take when developing such applications? A voodoo bonus for any pointers to useful training resources and explanations.

Thanks!

+6
source share
1 answer

If the method you want to use is in plugin1 , you would Export plugin1 . You can also Export use the method if you just want to share this method.

plugin2 will be Import plugin1. MEF takes care of the rest. If you get assemblies from the command line and create an AssemblyCatalog for plugin1 , you will also need to tell plugin2 about it and get it in AggregateCatalog or put them in a directory and use DirectoryCatalog (you can pass the directory name on the command line).

The MEF Programming Guide describes similar scenarios.

There is also a "Hands-On-Labs" for the MEF in Visual Studio 2010 and .NET Framework 4 Kit UEFA Training , which is useful.

+2
source

Source: https://habr.com/ru/post/898785/


All Articles