public interface ISomething
{
string SomeMethod(string arg);
}
public class Something : ISomething
{
public Something(Type type)
{
// initialization using type argument
}
public Something(string name)
{
// initialization using name argument
}
public string SomeMethod(string arg)
{
// do something
}
}
public class SomethingElse : ISomethingElse
{
public SomethingElse(ISomething something)
{
// ....
}
}
public class WindsorInstaller : IWindsorInstaller
{
public void Install(IWindsorContainer container, IConfigurationStore store)
{
container.Register(Component.For<ISomething>().ImplementedBy<Something>().Named("Something").LifestyleSingleton());
}
}
(爲簡便起見,我離開了標準溫莎初始化代碼。)Windsor構造函數注入可能帶有構造函數參數的類?
但當ISomethingElse
一個實例被創建(也許是由於ISomethingElse
被注入在其他一些類),溫莎無法解析ISomething
構造函數參數,因爲它不知道要爲type參數提供什麼。
Castle.MicroKernel.Handlers.HandlerException was unhandled by user code
HelpLink=groups.google.com/group/castle-project-users
HResult=-2146233088
Message=Can't create component 'Something' as it has dependencies to be satisfied.
'Something' is waiting for the following dependencies:
- Service 'System.Type' which was not registered.
- Parameter 'name' which was not provided. Did you forget to set the dependency?
你知道作爲參數調用'WindsorInstaller'時所使用的類型? – nemesv
我知道類型,但問題是ISomething,Something和他們的WindsorInstaller實際上是在一個單獨的項目中,它是作爲NuGet包構建的,並且由使用SomethingElse的項目訂閱。 – BaltoStar