没有装箱或类型参数转换 [英] There is no boxing or type parameter conversion

查看:45
本文介绍了没有装箱或类型参数转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到以下错误:

错误1类型'T'不能用作类型参数'T'在 泛型类型或方法'ClassLibrary1.A'.没有拳击或 类型参数从"T"转换为"ClassLibrary1.ALayer".

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>();
    }
}

这是什么错误?

任何帮助将不胜感激

答案 我必须明确命名约束

Answer I had to explicitly name the constraint

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

推荐答案

我必须明确命名对中产阶级的约束

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; }
}

这篇关于没有装箱或类型参数转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆