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

时间:2013-11-15 14:23:36

标签: c# generics

我已经实现了以下分层数据结构:树< T> →分支< T> →T

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

以下是示例Tree s:

*
├─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等子类型非常重要。

3 个答案:

答案 0 :(得分:3)

你不能真的。这是一个在泛型中称为协方差和逆变的问题。 你不能把长颈鹿和杂物塞进一个动物名单中,并希望一切都会好起来因为你的收藏品是动物名单。

有很多关于这个问题的文章,我不会详细描述。只需查看MSDN或google其他文章。

答案 1 :(得分:3)

一个常见的集合只知道 一种元素。就像List<int>知道其元素为int一样,List<T>知道其元素为T

因此,如果您知道所有树木都属于相同类型,但您不知道哪个,则必须从该类型中抽象出来:< / p>

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

和“你走了”。当然,当尝试将异构树放入该列表时,它将无法工作,因为T一种类型的项

因此,像List这样的集合只知道它们的一种类型的项目,在将异构对象放入一个公共列表/集合之前,你必须找到所有这些对象的“共同点”。

最简单的共同点是object,当然,它是。{因此,最愚蠢和始终如一的方法是:

List<object>

你可以放任何树木。

但是,当然,你也不想听。它只是向你展示共同点。

你的树有什么共同点?正如你所说 - 没有。当然,它们是Trees<T>又名Tree``1(应该是一个反引号,但我不知道如何在这里写它),但请注意,除了极少数情况,你不能使用非参数化类型{{在C#中1}}并且,如果不将T定义为某些内容,则无法使用Tree<>。定义后,Tree<T>Tree<T1>将被视为两个单独的类层次结构,除非T1 == T2或除非您使用某些Tree<T2>方差说明符(如in/out },因此IEnumerable<out> 可投射IEnumerable<Kangaroo>)。我假设你想要一个混合方差,所以不是一个选择。但是如果您希望树只是输入或仅输出,请立即使用co / contravariance说明符!

所以,让我们留下仿制品及其差异。为了向C#语言/编译器提供可理解的公共基础,你必须引入更多基本的东西。

IEnumerable<object>以外的常见abstract基础,一些常见的object。无论你认为什么都不脏。

当我真的必须混合和存储不同类型的泛型时,我通常会介绍一个简单的双界面:

interface

现在我可以将每个Thing转换为常见的IMyThing并将它们存储为混合的无类型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;}} } 。由于显式接口实现,我不会看到任何“无类型”成员,除非我实际投射它,所以其他T知晓的地方将像往常一样。

但是,当我必须混合类型时,我现在可以将它们转换为常见的无类型接口,并且由于接口,我仍然可以访问操作/属性。假设我将以某种方式保证args可以转换为正确的T.但是我总是可以检查TypeParam并手动处理它。

所以,纯粹的滋扰。如果感觉不对,请注意List<IMyThing>IEnumerable<T>完全一样!

呼叫IEnumerable,但实际上,没有动态(即没有DLR的.Net 3.5平台?),其他解决方案的空间很小。

答案 2 :(得分:-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() 
};
相关问题