温莎城堡: - 通过配置注入接口字典

use*_*190 3 c# castle-windsor inversion-of-control

嗨,我正在尝试注入接口字典,但我从这样的城堡得到一个错误: -

Castle.MicroKernel.SubSystems.Conversion.ConverterException:没有注册转换器来处理类型IFoo

为了绕过异常,我必须创建一个包含Ifoo接口列表的包装器并使用属性返回它.然后在配置==>字典而不是字典中使用包装器

在城堡中有没有办法,我可以只有一个Interface的字典而不是这个解决方法?

public interface IFoo {}
public class Foo {}
public class IfooWrapper {
    IList<IFoo> container{get;set;}
}
Run Code Online (Sandbox Code Playgroud)

Mau*_*fer 6

这对我来说很好(Windsor 2.0):

namespace WindsorTests {
    public interface IService {}    
    public class Service1 : IService {}    
    public class Service2 : IService {}    
    public class Consumer {
        private readonly IDictionary<string, IService> services;    
        public IDictionary<string, IService> Services {
            get { return services; }
        }    
        public Consumer(IDictionary<string, IService> services) {
            this.services = services;
        }
    }    

    [TestFixture]
    public class WindsorTests {    
        [Test]
        public void DictTest() {
            var container = new WindsorContainer(new XmlInterpreter(new StaticContentResource(@"<castle>
<components>
    <component id=""service1"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service1, MyAssembly""/>
    <component id=""service2"" service=""WindsorTests.IService, MyAssembly"" type=""WindsorTests.Service2, MyAssembly""/>
    <component id=""consumer"" type=""WindsorTests.Consumer, MyAssembly"">
        <parameters>
            <services>
                <dictionary>
                    <entry key=""one"">${service1}</entry>
                    <entry key=""two"">${service2}</entry>
                </dictionary>
            </services>
        </parameters>
    </component>
</components>
</castle>")));
            var consumer = container.Resolve<Consumer>();
            Assert.AreEqual(2, consumer.Services.Count);
            Assert.IsInstanceOfType(typeof(Service1), consumer.Services["one"]);
            Assert.IsInstanceOfType(typeof(Service2), consumer.Services["two"]);
        }
    }
}
Run Code Online (Sandbox Code Playgroud)