C# 无法隐式转换类型';B<;A>';至';B<;IA>';?

C# 无法隐式转换类型';B<;A>';至';B<;IA>';?,c#,autofac,c#-5.0,C#,Autofac,C# 5.0,我正在做一件非常简单的事情,但没有工作。我有 public class A : IA { } public interface IA { } public class B<T> where T : IA { } 公共A类:IA { } 公共接口 { } 公共B类,其中T:IA { } 现在我用Autofac注册这个 builder.Register<B<IA>>(b => { return

我正在做一件非常简单的事情,但没有工作。我有

public class A : IA
{
}

public interface IA
{
}

public class B<T> where T : IA
{

}
公共A类:IA
{
}
公共接口
{
}
公共B类,其中T:IA
{
}
现在我用Autofac注册这个

        builder.Register<B<IA>>(b =>
        {
            return new B<A>();
        });
builder.Register(b=>
{
返回新的B();
});
但是我得到了这个错误

Cannot implicitly convert type 'B<A>' to 'B<IA>'?

Cannot convert lambda expression to delegate type 'System.Func<Autofac.IComponentContext,B<IA>>' because some of the return types in the block are not implicitly convertible to the delegate return type
无法将类型“B”隐式转换为“B”?
无法将lambda表达式转换为委托类型“System.Func”,因为块中的某些返回类型不能隐式转换为委托返回类型

考虑到您正在使用的类,看起来您只需要允许编译器确定
寄存器的泛型参数:

builder.Register(b => new B<A>());

builder.Register(b=>new b,它允许通过
RegisterGeneric()
方法很好地控制注册的泛型类型。

我认为您缺少该功能

公共A类:IA
{
}
公共接口
{
}
公共B类,其中T:IA
{
}
相关链接:

public interface IB
{
}

public class B<T> : IB where T : IA
{

}
   public class A : IA
   {
   }

   public interface IA<out T>
   {
   }

   public class B where T : IA
   {
   }