Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

There is no boxing or type parameter conversion

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; }
}
like image 618
svillalta Avatar asked Feb 19 '15 17:02

svillalta


1 Answers

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; }
}
like image 109
svillalta Avatar answered Nov 15 '22 21:11

svillalta