1

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

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.