Pacman
Pacman

Reputation: 2245

Create MEF part with Constructor parameters

I have a class that export

[Export(typeof(IService))]

this class has a constructor that accept

Dictionary<string, object>
public MyService(Dictionary<string, object> model){}

this is how I create parts

var catalog = new AggregateCatalog();
catalog.Catalogs.Add("c:\\[SomePath]"));   
Container = new CompositionContainer(catalog);   
Container.ComposeParts(this);
return Container.GetExportedValue<IService>();

How do I go about injecting my paramter into my constructor when the part is exported ?

Upvotes: 1

Views: 1276

Answers (1)

Matt Burland
Matt Burland

Reputation: 45135

Container.ComposeExportedValue(model);

Assuming model is the Dictionary<string, object> that you want to inject.

Then you need to decorate the constructor for MyService with:

[ImportingConstructor]
public MyService(Dictionary<string, object> model){}

Upvotes: 3

Related Questions