Windsor构造函数注入了构造函数参数的类


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());
    }
}

(简而言之

但是,当创建ISomethingElse的实例(也许是由于ISomethingElse在其他类中注入)时,温莎无法解析ISomething构造函数参数,因为它不知道为类型参数提供什么。

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?

在这里查看,这显示了如何添加非服务的依赖项(因此无法通过容器解决)

最新更新