I have:
[Dependency]
public qwe property { get; set; }
And:
class qwe
{
public qwe()
{
MessageBox.Show("qwe");
}
public qwe(int x)
{
MessageBox.Show("qwe INT");
}
}
How can I configure Unity so that when I have registered an int Unity creates my class qwe with the constructor that takes an int, but when I haven't registered an int value, Unity uses the constructor with zero parameters.
Sebastian Weber
6,7182 gold badges32 silver badges51 bronze badges
asked Feb 29, 2012 at 17:47
Never
3232 gold badges7 silver badges24 bronze badges
1 Answer 1
You can either configure Unity to use the default constructor, the constructor that takes an integer parameter or declare a factory function that does that.
Default c'tor:
container.RegisterType<qwe>(new InjectionConstructor());
c'tor with integer parameter:
container.RegisterType<qwe>(new InjectionConstructor(myIntValue));
Factory:
container.RegisterType<qwe>(new InjectionFactory(c => (myIntValue > 0) ? new qwe(myIntValue) : new qwe()));
answered Mar 1, 2012 at 6:36
Sebastian Weber
6,7182 gold badges32 silver badges51 bronze badges
Sign up to request clarification or add additional context in comments.
Comments
lang-cs