简体   繁体   English

没有从“类型”到“接口”的隐式引用转换

[英]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 . 它实现了IAmAnInterfaceOneIAmAnInterfaceTwo

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. 实际上,您可以向IAmAnInterfaceOneIAmAnInterfaceTwo包含的组合接口添加更多要求。

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.

相关问题 用户定义类型从引用类型到接口的隐式引用转换 - Implicit Reference Conversion from reference type to interface for user defined types 使用继承接口的类型约束会导致隐式引用转换错误 - Using type constraints from inherited interface gives implicit reference conversion error 通用接口具体类型之间没有隐式引用转换错误 - No implicit reference conversion error between generic interface concrete type 没有从“type1”到“type2”的隐式引用转换 - There is no implicit reference conversion from 'type1' to 'type2' 在泛型中没有从“派生类型”到“基本类型”的隐式引用转换 - There is no implicit reference conversion from “Derive Type” to “Base type” in Generics 如何将接口的隐式转换写入另一种类型? - How to write Implicit Conversion from an Interface to another type? 从具有类型约束的类继承-“不存在来自…的隐式引用转换” - Inheriting from class with type constraints - “There is no implicit reference conversion from…” c#类型推断 - 错误 - 没有隐式引用转换 - c# Type Inference - Error - There is no implicit reference conversion from 通用参数基类型:“没有从B到A的隐式引用转换” - Generic parameter base type: “There is no implicit reference conversion from B to A” 泛型类型的隐式转换对于接口类型失败 - Implicit conversion with a generic type failing for an interface type
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM