2

给定一个接口

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<Foo, Bar>'

没有为 ISomething<Foo, Bar> 指定配置

1.) Container.GetInstance(ISomething<Foo, Bar>)

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

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

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

我错过了什么?

4

0 回答 0