drtf
drtf

Reputation: 1966

Using MEF with generic base class

I'm trying to figure how I can use MEF to import derived classes of a generic baseclass. This is a general description of the classes:

interface IPlugin {}

abstract class PluginBase<T> : IPlugin {}

[Export(typeof(IPlugin))]
class Plugin1 : PluginBase<T1> {

    public Plugin1(T1 t1) : base(t1) {};

}

[Export(typeof(IPlugin))]
class Plugin2 : PluginBase<T2> {

    public Plugin1(T2 t2) : base(t2) {};
}

[ImportMany(typeof(IPlugin))]
List<IPlugin> importedList { get; set; }

I'm using the following code in order to load the plugins:

 var aggCatalog = new AggregateCatalog(catalogList);
 var container = new CompositionContainer(aggCatalog, 
            CompositionOptions.DisableSilentRejection);
 container.ComposeParts(this);

But I get an empty list.

Upvotes: 2

Views: 1522

Answers (1)

Roman Zavalov
Roman Zavalov

Reputation: 585

Your types have non-default constructors so MEF cannot construct objects for you. Add [ImportingConstructor] attribute to constructors. And don't forget to add T1 and T2 objects into the container.

Upvotes: 6

Related Questions