在C#中创建复合通用子类型的通用列表

Mar*_*cel 7 c# generics

我实现了以下分层数据结构:Tree <T>→Branch <T>→T.

更新:很多人问:为什么不使用对象代替<T>(或<dynamic>或其他)?所以我修改了我的问题来陈述我的"约束".开始了...

这是一个例子Tree:

*
??Negative
? ??-2
??0
? ??0
??Positive
? ??2
? ??12
? ??2147483647

*
??Spring
? ??Mar
? ??Apr
? ??May
??Summer
? ??Jun
? ??Jul
? ??Aug
??Fall
? ??Sep
? ??Oct
? ??Nov
??Winter
? ??Dec
? ??Jan
? ??Feb
Run Code Online (Sandbox Code Playgroud)

在C#中实现:

public class Tree<T>
{
    public readonly List<Branch<T>> Branches = new List<Branch<T>>();
}

public class Branch<T>
{
    public readonly List<T> Leaves = new List<T>();
    public string Name { get; set; }
}

public class StringLeaf
{
    public StringLeaf(string value) { Label = value; }
    public string Label { get; private set; }
    public override string ToString() { return Label; }
}

public class PositiveIntLeaf
{
    private readonly int _value;
    public PositiveIntLeaf(int value) { _value = value; }

    public string Value
    {
        get { return _value < 0 ? "-" : _value.ToString(); }
    }
    public override string ToString() { return Value; }
}

public class IntTree : Tree<IntLeaf>
{
    private readonly Branch<IntLeaf> _negatives = new Branch<IntLeaf> { Name = "Negative" };
    private readonly Branch<IntLeaf> _zeros = new Branch<IntLeaf> { Name = "0" };
    private readonly Branch<IntLeaf> _positives = new Branch<IntLeaf> { Name = "Positive" };

    public IntTree()
    {
        Branches.AddRange(new []{
            _negatives,
            _zeros,
            _positives
        });
    }

    public void Add(int value)
    {
        if (value < 0) _negatives.Leaves.Add(new IntLeaf(value));
        else if (value > 0) _positives.Leaves.Add(new IntLeaf(value));
        else _zeros.Leaves.Add(new IntLeaf(value));
    }
}
Run Code Online (Sandbox Code Playgroud)

假设我有不同的树,我无法将它们列入一个列表:

IntTreeintTree = new IntTree();
intTree.Add(-2); intTree.Add(2); intTree.Add(0); intTree.Add(12); intTree.Add(int.MaxValue);
Tree<StringLeaf> months = new Tree<StringLeaf>{ Branches =
{
    new Branch<StringLeaf> { Name = "Spring", Leaves = { new StringLeaf( "Mar"),new StringLeaf("Apr") ,new StringLeaf("May")} },
    new Branch<StringLeaf> { Name = "Summer", Leaves = {  new StringLeaf( "Jun"),new StringLeaf("Jul") ,new StringLeaf("Aug")} },
    new Branch<StringLeaf> { Name = "Fall", Leaves = { new StringLeaf( "Sep"),new StringLeaf("Oct") ,new StringLeaf("Nov")} },
    new Branch<StringLeaf> { Name = "Winter", Leaves = { new StringLeaf( "Dec"),new StringLeaf("Jan") ,new StringLeaf("Feb")} }
}};

var list = new [] { intTree, months };
var currentTree = list[0];
// Work with the current tree:
var count = currentTree.Branches.Count;
Display(currentTree);
Run Code Online (Sandbox Code Playgroud)

错误是:找不到隐式类型数组的最佳类型

我怎样才能从这些树中得到一份清单?

我想强调一点,我只是想把它们放在一个列表中,可能会遍历它并访问当前树及其所有分支(例如显示它们的名称).我不关心T是对象还是抽象基类!假设我只是打电话.ToString().具体类型对于类似的子类型非常重要IntTree.

Ond*_*cek 4

你真的不能。这是泛型中称为协变和逆变的问题。你不能把长颈鹿和老虎塞进一个动物列表中,并希望一切都会好起来,只是因为你的收藏是一个动物列表。

关于这个问题的文章很多,我就不详细描述了。只需查看MSDN或谷歌其他文章即可。

  • 我并没有对你投反对票——只是想澄清这一点。当OP询问“如何从所有这些树中获取列表?”时,这是可能的——您只需将它们声明为协变接口即可。这不是一个完美的解决方案,因为协方差对于值类型失败,但如果您愿意付出装箱代价,则可以在此处使用协方差。 (2认同)