StructureMap:注册泛型类型作为通用接口

时间:2017-10-31 23:24:48

标签: c# generics dependency-injection structuremap structuremap4

给定一个接口

public interface ISomething<TA, TB>
    where TA : class
    where TB : class, ISomeOtherInterface
{
    object SomeMethod(TSource source, TDestination destination);
}

实施它的课程

public class Something<TA, TB> : ISomething<TA, TB>
    where TA : class
    where TB : class, ISomeOtherInterface
{
    public object SomeMethod(TA source, TB destination)
    {
        // lots of really important code.
    }
}

我尝试通过For().Use()ConnectImplementationsToTypesClosing()

注册实施
var container = new Container();

container.Configure(config => {
    config.For(typeof(ISomething<,>))
          .Use(typeof( Something<,>));

    config.Scan(_ => {
        _.AssemblyContainingType(typeof(Something<,>));
        _.ConnectImplementationsToTypesClosing(typeof(ISomething<,>));
    });
});

但无济于事;每次拨打container.GetInstance<ISomething<Foo, Bar>>()都会失败并显示StructureMapConfigurationException

  

没有注册默认实例,无法自动确定类型'ISomething&lt; Foo,Bar&gt;'

     

没有为ISomething&lt; Foo,Bar&gt;

指定配置      

1。)Container.GetInstance(ISomething&lt; Foo,Bar&gt;)

在同一个容器上调用container.WhatDoIHave()会产生

ISomething<TA, TB> My.Project.Namespace Transient Something<TA, TB> (Default)

虽然。此外,调用container.GetInstance<Something<Foo, Bar>>()(引用实现而不是接口)可以正常工作。

我错过了什么?

0 个答案:

没有答案