考虑以下结构作为 Autofac 3.0.0 的注册主体:
class Something
{
public int Result { get; set; }
}
class SomethingGood : Something
{
private int _good;
public int GoodResult {
get { return _good + Result; }
set { _good = value; }
}
}
interface IDo<in T> where T : Something
{
int Calculate( T input );
}
class MakeSomethingGood : IDo<SomethingGood>
{
public int Calculate( SomethingGood input ) {
return input.GoodResult;
}
}
class ControlSomething
{
private readonly IDo<Something> _doer;
public ControlSomething( IDo<Something> doer ) {
_doer = doer;
}
public void Show() {
Console.WriteLine( _doer.Calculate( new Something { Result = 5 } ) );
}
}
我正在尝试注册具体类型 MakeSomethingGood ,然后通过逆变接口解决它。
var builder = new ContainerBuilder();
builder.Register( c => new MakeSomethingGood() ).As<IDo<SomethingGood>>();
builder.Register( c => new ControlSomething( c.Resolve<IDo<Something>>() ) ).AsSelf();
var container = builder.Build();
var controller = container.Resolve<ControlSomething>();
...并Resolve
失败,因为没有找到组件IDo<Something>
我究竟做错了什么?
谢谢