gpt4 book ai didi

interface - 如何在 StructureMap 中注册通用接口(interface)

转载 作者:行者123 更新时间:2023-12-04 17:28:57 25 4
gpt4 key购买 nike

如何在结构化映射中注册通用接口(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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com