在具有继承和泛型的 dotnet 核心中无法进行转换

Conversion not possible in dotnet core with inheritance and generics

这是我当前的代码(.net core):

箱码:

class Box { }

SpeicalBox代码:

class SpecialBox : Box { }

堆栈代码:

interface Stack<T> where T : Box 
{
    AnotherInterface<T> TheFunction();
}

特殊堆栈代码:

class SpecialStack : Stack<SpecialBox> { }

StacksHolder 代码:

class StacksHolder
{
    private List<Stack<Box>> Stacks = new List<Stack<Box>>();
}

这是我得到的错误:

如果我尝试将 SpecialStack 添加到 StacksHolder 中的堆栈列表,我会收到以下错误:

cannot convert from 'SpecialStack' to 'Stack'

我使用的代码:

class StacksHolder
{
    private List<Stack<Box>> Stacks = new List<Stack<Box>>();

    public StacksHolder()
    {
        Stacks.Add(new SpecialStack());
    }
}

有谁知道为什么这不起作用? 如果有人能向我解释如何修复它,我将非常高兴。

提前致谢!

我可以通过向接口 StackAnotherStack 添加 out 关键字来解决我的问题。

谢谢@Lee 带我解决问题![​​=15=]

解决方案代码:

堆栈代码:

interface Stack<out T> where T : Box 
{
    AnotherInterface<T> TheFunction();
}

另一个接口代码:

interface AnotherInterface<out T> where T : Box
{
}