Search code examples
c#genericsdependency-injectioninversion-of-controlmef

Importing MEF plugins into DI/IOC container


I am trying to use MEF to load plugins into a DI framework. There are multiple types such as:

ISocket
IBroadcastSocket
INodeTask

Currently I have setup my code this way:

[Import]
INodeTask NodeTaskPlugin { get; set; }

And trying to import the plugin like so:

Injector.Instance.Bind<typeof(INodeTask), NodeTaskPlugin.GetType()>();

When trying the above line of code, R# complains that it cannot find the right method:

Cannot find method group. Did you intend to invoke the method?

Here is the Bind declaration that I wish to use:

void Bind<TBase, TDerived>() where TDerived : TBase

My question is then two fold:

  1. How do I solve the above error?
  2. Is there a more efficient way of importing plugins into a IoC container?

Solution

  • Use the non-generic Bind overload of your DI container:

    Bind(typeof(INodeTask), NodeTaskPlugin.GetType());