Can I Mark prefered .ctor to use for resolution with attribute

Sep 16, 2012 at 8:34 AM

Hello, is there a way to mark a constructor of a type to be resolved by MetroIoc as the one to be used ?

Usually, with other IoC containers, the strategy is : resolve the one with the greatest number of dependencies which can be satisfied by the IoC container.

But also, it is possible to "force" resolving a type with less arguments :

Usually it is done through the use of an attribute.

Is this something that can be achieved in MetroIoC ?


Sep 17, 2012 at 3:16 AM


That doesn't exist at the moment, but it would be an easy thing to patch in. I'll add it to the list.


thanks, Ian

Oct 1, 2012 at 10:36 AM

Hi, I've added this in now - you can mark a constructor as [PreferredConstructor] to ensure the container uses it.

Oct 1, 2012 at 1:41 PM

Can you also add a way to register the preferred constructor without using an attribute in classes?

Oct 3, 2012 at 12:19 AM

@powerdude - Not sure how that would work? You can always new up an instance of the object (using whichever constructor you like) and register that in the container...

Oct 3, 2012 at 1:14 PM
Edited Oct 3, 2012 at 1:14 PM

The goal would be something like this:


container.RegisterType<IMyType, MyType>( new PreferredConstructor(type1, type2));


where you "tell" the container what constructor to use if necessary, versus polluting classes with IOC attributes or having to new the objects yourself.