如何在结构化映射中注册通用接口(interface)的所有实例?
我知道如何为非通用接口(interface)执行此操作:
internal class MVCDemoRegistry : Registry
{
public MVCDemoRegistry()
{
Scan(x =>
{
x.Assembly("MVCDemo");
x.Assembly("MVCDemo.Infrastructure");
x.Assembly("MVCDemo.Services");
x.AddAllTypesOf<ISupplyView>();
});
}
}
最佳答案
我会选择类似的东西
// in IToaster.cs
public interface IToaster<T> {}
// in your StructureMap registry
Scan(x =>
{
x.Assembly("MVCDemo");
x.Assembly("MVCDemo.Infrastructure");
x.Assembly("MVCDemo.Services");
x.AddAllTypesOf(typeof(IToaster<>))
});
这里的关键是这种方法使用了 AddAllTypesOf() 的非泛型重载。否则,这确实会成为一个粘性小部件。
有关这些问题的良好讨论,请参阅此 SO 线程:StructureMap Auto registration for generic types using Scan
这应该可以解决问题,除非我缺少您的方法 - 如果有,请随时更新。
关于interface - 如何在 StructureMap 中注册通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1144204/