问题
I am learning castle.windsor following the tutorial online. this is the simple sample code:
public class Form1 {
private readonly HttpServiceWatcher serviceWatcher;
private System.ComponentModel.Container components = null;
public Form1()
{
InitializeComponent();
}
public Form1(HttpServiceWatcher serviceWatcher) : this()
{
this.serviceWatcher = serviceWatcher;
}
}
HttpServiceWatcher is in the xml conf file.
My question is: who is calling the constructor that has the parameter: public Form1(Http....)
?
at the program.cs i have this:
container.AddComponent("form.component",typeof(Form1));
Form1 form1 = (Form1) container["form.component"];
Application.Run(form1);
回答1:
The container calls the constructor when it creates the requested object. The constructor that gets called is the constructor with the most arguments that the container can satisfy.
回答2:
The dependency container itself creates the object (and thus calls the constructor).
来源:https://stackoverflow.com/questions/464299/who-calls-the-constructor-witth-parameter-castle-windsor