gpt4 book ai didi

c# - StructureMap - Scan - 具有基本实现和特定的通用接口(interface)

转载 作者:行者123 更新时间:2023-11-30 22:46:28 25 4
gpt4 key购买 nike

我有一个类似这样的界面:

interface IGenericSetupViewModel<T>

然后我有一个默认的实现,像这样

class GenericSetupViewModel<T> : IGenericSetupViewModel<T>

对于某些特定的类,我有这样的特定实现:

class ContractSetupViewModel : GenericSetupViewModel<Contract>

现在我想让 StructureMap 在请求

时返回正确的实例
ObjectFactory.GetInstance<GenericSetupViewModel<Contract>();

我想返回 ContractSetupViewModel,当要求其他任何东西时,我想得到一个实例

GenericSetupViewModel<T>

我试过这样做:

        StructureMap.ObjectFactory.Configure(x =>
{
x.Scan(y =>
{
y.TheCallingAssembly();
y.AddAllTypesOf(typeof(IGenericSetupViewModel<>));
y.ConnectImplementationsToTypesClosing(typeof(IGenericSetupViewModel<>));
});
});

然而,这导致我总是得到一个 GenericSetupViewModel 而不是 ContractSetupViewModel。我不想指定所有特定的 View 模型,所以无论如何我可以让这个扫描工作吗?

最佳答案

StructureMap 中存在一个短暂的错误,如果您的特定关闭类型没有直接实现接口(interface),ConnectImplementationToTypesClosing 就会出现问题。获得最新版本的 StructureMap 后,以下代码将起作用:

StructureMap.ObjectFactory.Configure(x =>
{
x.Scan(y =>
{
y.TheCallingAssembly();
y.ConnectImplementationsToTypesClosing(typeof(IGenericSetupViewModel<>));
});
x.For(typeof (IGenericSetupViewModel<>)).Use(typeof(GenericSetupViewModel<>));
});

关于c# - StructureMap - Scan - 具有基本实现和特定的通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620650/

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