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

7
我已经实现了以下分层数据结构: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

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

假设我有不同的树,我无法将它们放入列表中
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);

错误是:未找到隐式类型数组的最佳类型 如何从所有这些树中获取列表? 我想强调的是,我只想把它们放在一个列表中,可能会迭代它并访问当前树及其所有分支(例如,显示它们的名称)。我不关心T是对象还是抽象基类!假设我只调用.ToString()。具体类型仅对于子类型(如IntTree)很重要。

2
在这种情况下,为什么要使用泛型,当您似乎想在一个集合中拥有不同类型?ArrayList可能已经足够了吗? - Björn Roberg
http://blogs.msdn.com/b/csharpfaq/archive/2010/02/16/covariance-and-contravariance-faq.aspx - Alex
也许这只是一个毫无意义的评论,因为不了解你的限制条件,但是如果最终你只是将所有类都捆绑在一起,失去了泛型提供的类型安全性,那么保持类的通用性是否有任何意义呢? - decPL
阅读有关协变的内容。 - Luis Filipe
@decPL 现在保持类的通用性是有意义的。我希望... - Marcel
@Marcel 只有在你实际使用它们是泛型时,才会受益。如果你无法在编译时确定它们的类型,那么它们是泛型的事实就没有任何好处,反而会引入额外的复杂性。 - decPL
3个回答

4
你实际上无法这样做。这是泛型中的协变和逆变问题。 你不能将长颈鹿和老虎塞进动物列表中,然后希望一切都会好起来,仅仅因为你的集合是一个动物列表。
已经有很多关于这个问题的文章了,我不会详细描述它。只需查看MSDN或谷歌其他文章即可。

+1 因为识别到协方差是问题的根源;-1 因为建议无法完成。CLR 自 .NET 4 版本以来已支持泛型。 - Rob Lyndon
1
@RobLyndon 先生,您错了。CLR自.NET 2.0起就支持泛型,以及协变和逆变,但仅限于VB.NET。直到.NET 4.0发布之前,C#才支持协变的IEnumerable和逆变的委托。 - Ondrej Janacek
抱歉,那就是我想说的。可惜我没有说出来。当时我不知道VB支持协变和逆变。 - Rob Lyndon
@RobLyndon 好的,那么你的-1是因为声称无论如何都做不到吗?因为由于这些限制,按照提供的方式无法完成。 - Ondrej Janacek
2
我没有给你投反对票 - 只是想明确这一点。 当 OP 问“如何从所有这些树中获取列表?”时,这是可能的 - 您只需要将它们声明为协变接口即可。 这不是完美的解决方案,因为协变对于值类型失败,但如果您愿意支付装箱值的代价,则可以在此处使用协变。 - Rob Lyndon
好的,我本可以提到那个。没关系。另外,你的评论加一。 - Ondrej Janacek

3

一个通用的集合只会知道一种元素类型。就像 List<int> 知道它的元素是 intList<T> 知道它的元素是 T

所以,如果你知道所有树都是相同的类型,但不知道哪个类型,你必须从这个类型中抽象出来:

class MyTreeHandler<T>
{
    public List<Tree<T>> myListOfTrees = new List<Tree<T>>();
}

并“在这里你可以去”。 当尝试将异构树放入该列表中时,它当然不起作用,因为T是一种项目类型。因此,由于List等集合仅知道其项目的一种类型,因此在将异构对象放入一个共同的列表/集合之前,您必须找到所有这些对象的“共同点”。最简单的共同点是对象,当然是这样。因此,最愚蠢且始终有效的方法是:
List<object>

你可以在这里放置任何东西的树。

但是,毫无疑问,你也不想听到这个。这只是为了向你展示共同点。

你的树有什么共同点?按照你的描述 - 没有。当然,它们是 Trees<T> 或者 Tree``1(应该有一个反引号,但我不知道如何在这里写出来),但请注意,除非极少数情况,否则你不能在C#中使用非参数化类型 Tree<>。并且,在定义T之前,你不能使用 Tree<T>。在你定义它之后,Tree<T1>Tree<T2>将被视为两个单独的类层次结构,除非 T1==T2或者你使用一些输入/输出的变体说明符(就像 IEnumerable<out> 一样,因此 IEnumerable<Kangaroo> 可以强制转换为 IEnumerable<object>)。我假设你想要一个混合变量,所以这不是一个选项。但如果你希望你的树是只输入或只输出的,请立即使用协变/逆变说明符!

因此,让我们离开泛型及其变体。为了向C#语言/编译器提供一个通用的基础,你必须引入一些更基本的东西。

除了 object 之外的共同的抽象基类,一些通用的 interface。无论你认为哪个最不肮脏。

当我真正需要混合和存储不同类型的泛型时,我通常会引入一个简单的双接口:

public interface IMyThing //Typeless
{
    Type TypeParam {get;}

    object Operation(object a);
    object Property {get;set;}
}

public interface IMyThing<T> //TypeD
{
    T Operation(T a);
    T Property {get;set;}
}

public class MyThing<T> : IMyThing<T>, IMyThing
{
    public T Operation(T a) { .. }
    public T Property {get;set;}

    Type IMyThing.TypeParam {get{return typeof(T);}}
    object IMyThing.Operation(object a){return this.Operation((T)a);}
    object IMyThing.Property {get{return this.Property;}set{this.Property=(T)value;}}
}

现在,我可以将每个Thing转换为通用的IMyThing,并将它们存储为混合的、无类型的List。由于显式接口实现,除非我实际进行强制类型转换,否则我不会看到任何“无类型”成员,因此其他T感知的位置看起来与平常一样。
但是,当我必须混合类型时,我现在可以将它们强制转换为通用的无类型接口,由于该接口,我仍然可以访问操作/属性。假设我以某种方式保证args可以强制转换为正确的T。但是我始终可以检查TypeParam并手动处理它。
所以,这是一个纯粹的麻烦。尽管它感觉很错误,请注意,IEnumerable和IEnumerable完全相同!
人们呼吁使用dynamic,但事实上,在没有dynamic(即没有DLR的.Net 3.5平台)的情况下,几乎没有其他解决方案的空间。

这是一个很好的、常见的解决方案。然而,正如我在类似问题的答案中所提出的那样,这违反了Liskov替换原则。另一种方法是创建一个非泛型包装器,遵循适配器模式。 - Steven Jeuris

-1

另一种选择是使用 dynamic 类型。请尝试以下操作:

static void Main(string[] args)
{
    Tree<dynamic> intTree = CreateTree<dynamic>
        (1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 12, 15);
    Tree<dynamic> charTree = CreateTree<dynamic>
        ('1', '2', '3', '4', '5', '6', '7', '8', '9', 'A', 'C', 'E');
    Tree<dynamic> months = CreateTree<dynamic>
        ("Jan", "Feb", "Mar", "Apr", "May", "Jun", "Jul", "Aug", "Sep", "Oct", "Nov", "Dec");
    IEnumerable<Tree<object>> list = 
        new List<Tree<object>> { intTree, charTree, months };
}

private static Tree<T> CreateTree<T>(params T[] values)
{
    var t = new Tree<T>();
    var b = new Branch<T>();

    // some branching logic
    for (int i = 0; i < values.Length; i++)
    {
        if (i % 4 == 3)
        {
            b.Leaves.Add(values[i]);
            t.Branches.Add(b);
            b = new Branch<T>();
        }
        else

            b.Leaves.Add(values[i]);
    }
    if (b.Leaves.Count != 0)
        t.Branches.Add(b);

    return t;
}

你也可以为你的目的创建一个扩展方法:

public static Tree<dynamic> MakeDynamic<T>(this Tree<T> inputTree)
{
    var newTree = new Tree<dynamic>();
    for (int i = 0; i < inputTree.Branches.Count; i++)
    {
        var newBranch=new Branch<dynamic>();
        newTree.Branches.Add(newBranch);
        for (int j = 0; j < inputTree.Branches[i].Leaves.Count; j++)
        {
            dynamic d = inputTree.Branches[i].Leaves[j];
            inputTree.Branches[i].Leaves[j] = d;
        }
    }
    return newTree;
}

在这种情况下,您可以保留原始类型并使用以下示例:
IEnumerable<Tree<object>> list = new List<Tree<object>> { 
    intTree.MakeDynamic(), 
    charTree.MakeDynamic(), 
    months.MakeDynamic() 
};

6
如果你的树中每一个都变成了 Tree<dynamic>,那么从类中删除泛型类型,只保留 Tree 是否会更简单? - decPL
@Moo-Juice 那是匿名类型,不是 dynamic。完全不同。没有实际的对象属于 dynamic 类型。你不能新建一个 dynamic 对象。你只能有一个 dynamic 类型的变量,它可以容纳其中任何实际对象的实例。 - Servy
3
在这里使用“dynamic”是不必要的。与使用普通的“Tree”基类相比,它并没有增加任何价值。 - Rob Lyndon
1
当不存在类型安全的解决方案时,应该使用动态。但这并不是现在的情况。 - Luis Filipe

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接