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