我试图列出模板化类的列表,将基类传递给模板。但是,似乎不允许这样做。有没有办法避免这种限制,或者更适当地重组我的代码?

这是一个抽象的例子:

using System;
using System.Collections.Generic;

namespace TempInherit
{
    abstract class Shape{}

    class Triangle : Shape{}
    class Square : Shape{}

    class ShapeHolder<T>{}

    class MainClass
    {
        public static void Main(string[] args)
        {
            // list of base class, add subclass - works
            List<Shape> shapes = new List<Shape>();
            shapes.Add(new Triangle());
            shapes.Add(new Square());

            // list of holders of base class, add holders of subclass - fails
            List<ShapeHolder<Shape>> shapeHolders = new List<ShapeHolder<Shape>>();
            shapeHolders.Add(new ShapeHolder<Triangle>());
            shapeHolders.Add(new ShapeHolder<Square>());
        }
    }
}


产生:


  错误CS1502:最佳匹配的重载方法
  `System.Collections.Generic.List> .Add(TempInherit.ShapeHolder)'
  有一些无效的参数(CS1502)(TempInherit)
  
  错误CS1503:要键入的参数#1' cannot convert TempInherit.ShapeHolder'表达式
  `TempInherit.ShapeHolder'(CS1503)(TempInherit)

最佳答案

协方差问题:

您可以创建接口IShapeHolder<out T>,因为接口上的通用参数可以协变(但不能在类上)

这样的东西

public class Shape
    {
    }
    public class Triangle : Shape
    {
    }
    public class Square : Shape
    {
    }
    //T generic parameter is covariant (out keyword)
    public interface IShapeHolder<out T> where T : Shape
    {
    }
    public class ShapeHolder<T>  : IShapeHolder<T> where T: Shape
    {
    }


然后,

var shapes = new List<Shape>();
shapes.Add(new Triangle());
shapes.Add(new Square());

// list of holders of base class, add holders of subclass - fails no more
var shapeHolders = new List<IShapeHolder<Shape>>();
shapeHolders.Add(new ShapeHolder<Triangle>());
shapeHolders.Add(new ShapeHolder<Square>());

10-07 14:13