没有从 'Type' 到 'Interface' 的隐式引用转换

There is no implicit reference conversion from 'Type' to 'Interface'

我有三个接口:

internal interface IAmAnInterfaceOne
{
    int PropertyOne { get; set; }
}

internal interface IAmAnInterfaceTwo
{
    int PropertyTwo { get; set; }
}

internal interface IAmAnInterfaceCombiningOneAndTwo : IAmAnInterfaceOne, IAmAnInterfaceTwo
{
}

实施class:

internal class Myimplementation : IAmAnInterfaceOne, IAmAnInterfaceTwo
{
    public int PropertyOne { get; set; }
    public int PropertyTwo { get; set; }
}

当我尝试将接口 IAmAnInterfaceCombiningOneAndTwo 绑定到 Myimplementation 时,出现错误:

There is no implicit reference conversion from 'Type' to 'Interface'

class AppModule : NinjectModule
{
    public override void Load()
    {
        Bind<IAmAnInterfaceCombiningOneAndTwo>().To<Myimplementation>().InSingletonScope();
    }
}

我想像这样在构造函数中使用它:

public class MyClass
{
    private readonly IAmAnInterfaceCombiningOneAndTwo _param;

    public MyClass(IAmAnInterfaceCombiningOneAndTwo param)
    {
        _param = param;
    }
}

如何正确绑定接口? 谢谢。

这不是 Ninject 错误。

IAmAnInterfaceCombiningOneAndTwo obj = new Myimplementation();

以上代码无法编译。为什么?因为 Myimplementation 没有实现 IAmAnInterfaceCombiningOneAndTwo。它实现了 IAmAnInterfaceOneIAmAnInterfaceTwo.

IAmAnInterfaceCombiningOneAndTwo要求这两个都实现了,但是仅仅实现了不代表实现者实现了组合接口。从技术上讲,它是完全不同的界面。实际上,您可以向组合接口添加 IAmAnInterfaceOneIAmAnInterfaceTwo 都不包含的进一步要求。

最简单的解决方案是更改 Myimplementation 以显式声明它实现组合接口(另外,或仅实现该接口 - 净效果是相同的):

internal class Myimplementation : IAmAnInterfaceCombiningOneAndTwo