Tell me, can the host transfer some information to it when loading the plug-in or when creating only the default constructor is used?

If not, what are the alternatives?

  • And why not make a plugin a method for passing the same parameters and do not call it yourself? - Monk
  • @Monk, in my opinion, encapsulation will suffer. It turns out that the external component knows about the device of the plug-in that before using it is necessary to call a special method, and the designer is just a better solution. - iluxa1810
  • If this is declared in the interface of the plugin itself - there is no problem. Anyway, you usually need a single event BEFORE using the plug-in so that it can bring itself to a working state. If in your case it is not so - well, then it does not fit, yes. - Monk

1 answer 1

You can mark the desired constructor with the ImportingConstructor attribute. Only the container must contain both the argument and the plugin itself.

For example:

[Export(typeof(ILogger))] class Logger : ILogger {} [Export(typeof(IPlugin))] class Plugin : IPlugin { [ImportingConstructor] public Plugin(ILogger logger) { } } 

Catalog:

 var catalog = new TypeCatalog(typeof(Plugin), typeof(Logger));