C#中的循环类型

时间:2018-08-28 16:01:28

标签: c# generics

此代码无法编译:

public class Component<T1>
{
    public virtual void Foo(T1 t1)
    {
        return;
    }
}

public class Panel<T1>
    where T1: Component<Panel<T1>>
{
    public void Bar() {
        Console.WriteLine("Win");
    }
}

public class MyFormPanel : Panel<MyFormPanel.Form>
{
    public class Form : Component<MyFormPanel>
    {
        public override void Foo(MyFormPanel t1)
        {
            t1.Bar();
        }
    }
}

给出的错误是MyFromPanel.Form不能用作类型参数T1,因为没有从MyFormPanel.FormComponent<Panel<MyFormPanel.Form>>的隐式引用转换

这似乎使我感到困惑,因为Form继承自Component,而MyFormPanel是Panel。为什么会发生此错误?

0 个答案:

没有答案