Generics 使用泛型的属性的StructureMap setter注入

Generics 使用泛型的属性的StructureMap setter注入,generics,dependency-injection,structuremap,Generics,Dependency Injection,Structuremap,编辑:清除一些名称,使示例更易于阅读 这一类: public class EntranceService : IEntranceService { public IMyNotifier<Entrance> Notifier { get; set; } public EntranceService(IRepositoryConfigDb<Entrance> repo ) { this.repo = repo; } }

编辑:清除一些名称,使示例更易于阅读

这一类:

public class EntranceService : IEntranceService
{
    public IMyNotifier<Entrance> Notifier { get; set; }


    public EntranceService(IRepositoryConfigDb<Entrance> repo   )
    {
        this.repo = repo;
    }
}
公共类入口服务:IEntranceService
{
公共IMyNotifier通知程序{get;set;}
公共服务(IRepositoryConfigDb repo)
{
this.repo=回购;
}
}
我想通过setter注入使用要注入的通知程序属性。正如您所见,通知程序使用具有开放泛型类型的接口,因此我无法执行以下操作:

x.SetAllProperties(p => p.OfType<IMyNotifier<IEntity>>());
x.SetAllProperties(p=>p.OfType());
StructureMap不喜欢它。 这就是IMyNotifier的外观:

public interface  IMyNotifier<T> where  T : class, IEntity, new()
公共接口IMyNotifier,其中T:class,IEntity,new()
编辑:这导致我必须为IEntity的每一个具体实现设置一个规则,如下所示:

x.SetAllProperties(setter => setter.OfType<IMyNotifier<Entrance>>());
x.SetAllProperties(setter => setter.OfType<IMyNotifier<Something>>());
x.SetAllProperties(setter => setter.OfType<IMyNotifier<Endpoint>>());
x.SetAllProperties(setter => setter.OfType<IMyNotifier<Interaction>>());
x.SetAllProperties(setter => setter.OfType<IMyNotifier<Queue>>());
x.SetAllProperties(setter => setter.OfType<IMyNotifier<Group>>());
x.SetAllProperties(setter=>setter.OfType());
x、 SetAllProperties(setter=>setter.OfType());
x、 SetAllProperties(setter=>setter.OfType());
x、 SetAllProperties(setter=>setter.OfType());
x、 SetAllProperties(setter=>setter.OfType());
x、 SetAllProperties(setter=>setter.OfType());

感谢您的帮助

好吧,这并不难。。您只需使用稍微不同的方法查看要设置的属性

 x.SetAllProperties(
    p=> p.TypeMatches(
           t=>t.IsInterface 
              && t.IsGenericType 
              && t.GetGenericTypeDefinition() == typeof(IMyNotifier<>)
    )
 );
x.SetAllProperties(
p=>p.TypeMatches(
t=>t.i接口
&&t.IsGenericType
&&t.GetGenericTypeDefinition()==typeof(IMyNotifier)
)
);

这样做的目的是根据谓词查看属性类型。因此,我们看到它是一个接口,是泛型的,然后获取开放泛型,并将其与我们想要匹配的内容进行比较。

在大多数情况下,您应该支持构造函数注入而不是属性注入。在这种情况下,你需要使用财产注入有什么特别的原因吗?你好,史蒂文,我同意,但那是另一回事。定义Setter注入时,此问题特定于。