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