2

I'm studying MEF and I'm not able to resolve a problem.

I have a main application, called MainMEF, and a simple module, called SimpleModule. This one consists of a single UserControl which is loaded dynamically.

When MainMEF starts up, I would be able to pass to the module a reference to main application contained into MainMEF.

How could I fix this?

Myles Gray
  • 8,711
  • 7
  • 48
  • 70
Lorenzo B
  • 33,216
  • 24
  • 116
  • 190

1 Answers1

6

Lots of questions regarding this already. You could pass it after initialisation using a property: How do I populate a MEF plugin with data that is not hard coded into the assembly?

Or use MEF constructor parameters: MEF Constructor Parameters with Multiple Constructors

The export looks something like this:

[Export(typeof(ITest))]
class Test : ITest 
{
    void Test() 
    {  }

    [ImportingConstructor] //<- This is the key bit here
    void Test(object parameter) 
    {  }
}

Then when composing your catalog do this:

catalog.ComposeExportedValue( /* parameter here */);
catalog.ComposeParts(this);
Community
  • 1
  • 1
Tim
  • 7,746
  • 3
  • 49
  • 83
  • Thank you for your reply. Very helpful. Could you explain me these lines with more code? Take into account catalog. ### catalog.ComposeExportedValue( /* parameter here */); catalog.ComposeParts(this); ### What kind of type does it belong to? – Lorenzo B Jun 04 '10 at 08:10