我有一个类似这样的界面:
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 - 扫描 - 具有基本实现和特定功能的通用接口(interface),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2620650/