1
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? 
+0

你知道作为参数调用'WindsorInstaller'时所使用的类型? – nemesv

+0

我知道类型,但问题是ISomething,Something和他们的WindsorInstaller实际上是在一个单独的项目中,它是作为NuGet包构建的,并且由使用SomethingElse的项目订阅。 – BaltoStar

回答

2

看一看here,这显示了如何添加依赖不属于服务(因此不能由容器来解决)

+0

谢谢,这是非常好的信息。但是,在我的情况下,该组件及其注册包含在NuGet包中。在我的消费项目中,是否可以重新注册提供动态参数的组件? – BaltoStar

+0

刚刚意识到我可以明确地在消费项目中执行注册,所以现在已经解决了。 – BaltoStar