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被注入到其他类中),Windsor 无法解析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?
4

1 回答 1

2

看看这里,这显示了如何添加不是服务的依赖项(因此容器无法解决)

于 2014-01-22T20:34:47.483 回答