Multiple constructors

May 31, 2010 at 3:49 PM
Edited May 31, 2010 at 9:23 PM
i have a class with multiple constructors, like this:

[Export(typeof(ifoo))]
class foo : ifoo {
    void foo() { ... }
    [ImportingConstructor]
    void foo(object par1) { ... }
    ...
}

i am using catalog.ComposeExportedValue() when composing to suply the par1 value to 2nd constructor:

...
catalog.ComposeExportedValue(par1Value);
catalog.ComposeParts(this);
...

To hold the components i using List<Lazy<ifoo, ifoometadata>>, and to create the foo instance i using the Value property.

Everthing works fine, except that the 2nd constructor of the foo class is never called.

How do i select the constructor i want to use when MEF instantiates the class?

May 31, 2010 at 3:53 PM
You can also use [Import] on parameters of a constructor. For example: public class Test { public Test( [Import] IDependency dependency) { // ... } } Hope that helps.
May 31, 2010 at 4:17 PM
Yes, i tried that too, but dont work, only the parameter less constructor is called.