简体   繁体   English

接口作为泛型类型 - 方法如何返回子接口类型的值?

[英]Interface as generic type - how can method return value of subinterface type?

I have project in C# that represents table using two different types of row.我在 C# 中有项目,它使用两种不同类型的行表示表。 SimpleRow and BetterRow. SimpleRow 和 BetterRow。 SimpleRow implements interface ISimpleRow and BetterRow implements IBetterRow where ISimpleRow is superclass of IBetterRow (SimpleRow cannot be superclass of BetterRow directly because that design is a little more complicated then I describe here. All row's interfaces and classes have generic TCell (not important here). SimpleRow 实现接口 ISimpleRow 和 BetterRow 实现 IBetterRow,其中 ISimpleRow 是 IBetterRow 的超类(SimpleRow 不能直接是 BetterRow 的超类,因为这种设计比我在这里描述的要复杂一些。所有行的接口和类都有通用 TCell(这里不重要)。

Now I have SimpleTable<TRow> and BetterTable<TRow>: SimpleTable<TRow> .现在我有SimpleTable<TRow>BetterTable<TRow>: SimpleTable<TRow> It looks like:看起来像:

public interface ISimpleRow<TCell> {...}
public interface IBetterRow<TCell> : ISimpleRow<TCell> {...}
public class SimpleRow<TCell> : ISimpleRow<TCell> {...}
public class BetterRow<TCell> : SimpleRow<TCell>, IBetterRow<TCell> {...}

public class SimpleTable<TRow> where TRow : SimpleRow<ICell> {
...
   public virtual TRow getRow() {
      return new SimpleRow<ICell>();
   }
}
public class BetterTable<TRow> : SimpleTable<TRow> where TRow : SimpleRow<ICell> {
   ...
   public override TRow getRow() {
      return new BetterRow<ICell>();
   }
}

There is my problem (line "return BetterRow..."):这是我的问题(行“return BetterRow ...”):

Cannot implicitly convert type BetterRow to TRow无法将 BetterRow 类型隐式转换为 TRow

And also when I am trying to create an instance eg new BetterTable<BetterRow<ICell>>()而且当我试图创建一个实例时,例如new BetterTable<BetterRow<ICell>>()

The type BetterRow cannot be used as type parametr TRow in the generic type or method BetterRow. BetterRow 类型不能用作泛型类型或方法 BetterRow 中的类型参数 TRow。 There is no implicit reference conversion from BetterRow to SimpleRow没有从 BetterRow 到 SimpleRow 的隐式引用转换

Any idea how to solve this?知道如何解决这个问题吗? My method getRow() actually can return instance of one of two classes that implemented it's interface.我的方法 getRow() 实际上可以返回实现它的接口的两个类之一的实例。 There are 4 classes where to implements SimpleRow and two implements BetterRow.有 4 个类实现 SimpleRow,两个实现 BetterRow。 Method getRow returns one of them (SimpleRowA or SimpleRowB if TRow is ISimpleRow or BetterRowA or BetterB if TRow is IBetterRow>).方法 getRow 返回其中之一(如果 TRow 是 ISimpleRow,则返回 SimpleRowA 或 SimpleRowB;如果 TRow 是 IBetterRow>,则返回 BetterRowA 或 BetterB)。 But it is not important here, I guess.但我想这在这里并不重要。 I just wanted to mention it for case and to make you sure I cannot change return type of getRow method.我只是想提一下,以确保我无法更改 getRow 方法的返回类型。

You can't do exactly that case SimpleRow<ICell>() is not guaranteed to be any type of TRow caller can provide.您不能完全做到这种情况SimpleRow<ICell>()不能保证是TRow调用者可以提供的任何类型。 Though one option is to add new() constraint if it is suitable for you:虽然一种选择是在适合您的情况下添加new()约束:

public class SimpleTable<TRow> where TRow : SimpleRow<ICell>, new()
{

   public virtual TRow getRow()
   {
        return new TRow();
   }
}
public class BetterTable<TRow> : SimpleTable<TRow> where TRow : SimpleRow<ICell>, new()
{

   public override TRow getRow()
    {
        return new TRow();
    }
}

But in general it seems that you need to think about changing approach in general and without having full api it is hard to argue about that.但总的来说,您似乎需要考虑总体上改变方法,并且没有完整的 api 很难对此争论。

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

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