[英]There is no implicit reference conversion from 'Type' to 'Interface'
I have three interfaces: 我有三个接口:
internal interface IAmAnInterfaceOne
{
int PropertyOne { get; set; }
}
internal interface IAmAnInterfaceTwo
{
int PropertyTwo { get; set; }
}
internal interface IAmAnInterfaceCombiningOneAndTwo : IAmAnInterfaceOne, IAmAnInterfaceTwo
{
}
Implementation class: 实现类:
internal class Myimplementation : IAmAnInterfaceOne, IAmAnInterfaceTwo
{
public int PropertyOne { get; set; }
public int PropertyTwo { get; set; }
}
When I tried to bind interface IAmAnInterfaceCombiningOneAndTwo
to Myimplementation
, I got error: 当我尝试将接口
IAmAnInterfaceCombiningOneAndTwo
绑定到Myimplementation
,出现错误:
There is no implicit reference conversion from 'Type' to 'Interface'
没有从“类型”到“接口”的隐式引用转换
class AppModule : NinjectModule
{
public override void Load()
{
Bind<IAmAnInterfaceCombiningOneAndTwo>().To<Myimplementation>().InSingletonScope();
}
}
I want to use it in constructor like that: 我想像这样在构造函数中使用它:
public class MyClass
{
private readonly IAmAnInterfaceCombiningOneAndTwo _param;
public MyClass(IAmAnInterfaceCombiningOneAndTwo param)
{
_param = param;
}
}
How to bind interface properly? 如何正确绑定接口? Thanks.
谢谢。
This is not a Ninject error. 这不是Ninject错误。
IAmAnInterfaceCombiningOneAndTwo obj = new Myimplementation();
The above code wouldn't compile. 上面的代码无法编译。 Why?
为什么? Because
Myimplementation
doesn't implement IAmAnInterfaceCombiningOneAndTwo
. 因为
Myimplementation
没有实现IAmAnInterfaceCombiningOneAndTwo
。 It implements IAmAnInterfaceOne
and IAmAnInterfaceTwo
. 它实现了
IAmAnInterfaceOne
和IAmAnInterfaceTwo
。
IAmAnInterfaceCombiningOneAndTwo
requires that these two are implemented, but merely implementing them doesn't also mean that the implementer implements the combination interface. IAmAnInterfaceCombiningOneAndTwo
要求实现这两个,但是仅仅实现它们并不意味着实现者实现组合接口。 It's technically a different interface altogether. 从技术上讲,这是完全不同的界面。 Indeed, you could add further requirements to the combination interface that neither
IAmAnInterfaceOne
nor IAmAnInterfaceTwo
include. 实际上,您可以向
IAmAnInterfaceOne
和IAmAnInterfaceTwo
包含的组合接口添加更多要求。
The simplest solution is to change Myimplementation
to explicitly declare that it implements the combination interface (either additionally, or just that interface - the net effect is the same): 最简单的解决方案是更改
Myimplementation
以显式声明它实现了组合接口(附加地或仅该接口-最终效果是相同的):
internal class Myimplementation : IAmAnInterfaceCombiningOneAndTwo
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.