There is no boxing or type parameter conversion

2019-07-18 02:28发布

I'm getting the following error:

Error 1 The type 'T' cannot be used as type parameter 'T' in the generic type or method 'ClassLibrary1.A'. There is no boxing or type parameter conversion from 'T' to 'ClassLibrary1.ALayer'.

I'm trying to apply Generics to achieve a dynamic polymorphism with Lists and inheritance. I have the following class structure:

public abstract class ALayer
{
    public int Id { get; set; }
}

public abstract class A<T> where T: ALayer
{
    public List<T> Layers;
}

I'm getting the error in the declaration of the Middle class

public abstract class Middle<T>:A<T>
{
    public int middleAttr { get; set; }
}

This is my third layer of classes where I wanto to get the dynamic polymorphism

public class BLayer : ALayer
{
    public int Id { get; set; }
}

public class B:Middle<BLayer>
{
    public B() {
        this.Layers = new List<BLayer>();
    }
}

What is this error?

Any help will be much appreciated

Answer I had to explicitly name the constraint

public abstract class Middle<T>:A<T> where T: ALayer
{
    public int middleAttr { get; set; }
}

1条回答
家丑人穷心不美
2楼-- · 2019-07-18 03:27

I had to explicitly name the constraint on the Middle class

public abstract class Middle<T>:A<T> where T: ALayer
{
    public int middleAttr { get; set; }
}
查看更多
登录 后发表回答