温莎城堡解析词典<>失败

时间:2010-07-21 22:50:04

标签: generics dictionary castle-windsor

我做了以下事情:

    container.Register(Component.For<Dictionary<string, string>>()
                            .Instance(ServiceDictionaryInstance)
                            .Named("serviceDictionary"));

该类使用的组件是:

 public class BusinessService : IDecisionFilter
 {
     private readonly Dictionary<string, string> _ServiceDictionary;

  public BusinessService(IOtherInterface otherService, Dictionary<string, string> serviceDictionary)
  {
                    _OtherService = otherService,
      _ServiceDictionary = serviceDictionary;
  }
    }

然后Castle无法解析Dictionary组件:

失败:NotImplementedException 该方法或操作未实现。 失败:在Castle.MicroKernel.SubSystems.Conversion.GenericDictionaryConverter.PerformConversion(String value,Type targetType)

但如果我创建另一个类:

public class DictiornayType : Dictionary<string, string>
{ 
}

代替原始词典,一切都正确解决。

这与泛型类型有什么关系吗?

请告知。

1 个答案:

答案 0 :(得分:3)

问题是,字典实际上不是component/service。它是某个组件/服务的参数。因此,您可以删除字典注册并将字典实例作为参数传递给其注册中的BusinessService组件:

container.Register(Component.For<IDecisionFilter>()
                            .ImplementedBy<BusinessService>()
                            .DependsOn(new {
                                serviceDictionary = ServiceDictionaryInstance
                            }));

对于未来版本的Windsor,有一些关于removing this difference between parameters and services的讨论。