繁体   English   中英

C#接口错误:没有从类xxx到接口xxxx的隐式引用转换

[英]C# Interface Error: There is no implicit reference conversion from class xxx to interface xxxx

我在不同的命名空间下有以下类。 我的意思是,相同的3个类存在于不同的命名空间下。

public class A
{
   public int a { get; set; }
}

public class B
{
   public A objA { get; set; }
}

public class C
{
   public List<B> listBinC { get; set; }
}

为了在这些类的对象之间进行利用/操作,我想到编写一个接口包装器,例如

public interface iA
{
   int a { get; set; }
}

public interface iB<T> where T: iA
{
   T objA { get; set; }
}

public interface iC<T> where T : iB<iA>
{
   List<T> listBinC {get; set; }
}

之后,我将类定义更改为

public class A : iA
{
    public int a { get; set; }
}

public class B : iB<A>
{
    public A objA { get; set; }
}

class C : iC<B>
{
    public List<B> listBinC { get; set; }
}

编译时出现以下错误

The type 'Example.B' cannot be used as type parameter 'T' in the generic type or method 'Example.iC<T>'. 
There is no implicit reference conversion from 'Example.B' to 'Example.iB<Example.iA>'.

我无法更改班级结构,因为它是由其他团队提供的。 强制接口“约束”解决错误的正确方法是什么?

public interface iC<T, TI> 
    where T : iB<TI> 
    where TI : iA
{
   List<T> listBinC {get; set; }
}

在这里, iA作为单独的泛型参数被拉出,因此您可以对其施加约束(对于TIiA派生),然后像这样声明B

class C : iC<B, A>
{
    public List<B> listBinC { get; set; }
}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM