簡體   English   中英

在C#中創建復合通用子類型的通用列表

[英]Creating a generic list of composite generic subtypes in C#

我實現了以下分層數據結構: 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這樣的子類型非常重要。

你不能真的。 這是一個在泛型中稱為協方差和逆變的問題。 你不能把長頸鹿和雜物塞進一個動物名單中,並希望一切都會好起來因為你的收藏品是動物名單。

關於這個問題的文章很多,我不會詳細描述。 只需查看MSDN或google其他文章。

一個共同的集合將只知道 一種類型的元素 就像List<int>知道它的元素是intList<T>知道它的元素是T

所以,如果你知道你的所有樹都屬於同一類型,但你不知道哪個 ,你必須從那個類型中抽象出來:

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

和“你走了”。 當然,當嘗試將異構樹放入該列表時,它將無法工作,因為T一種項目

因此,像List這樣的集合只知道其項目的一種類型,在將異構對象放入一個公共列表/集合之前,必須為所有這些對象找到“共同點”。

最簡單的共同點是一個object ,當然,它是。 因此,最愚蠢和始終如一的方法是:

List<object>

你可以放任何樹木。

但是,當然,你也不想聽。 這只是為了向您展示共同點。

你的樹有什么共同點? 正如你所說 - 沒有。 當然,它們是Trees<T> aka Tree``1 (應該是一個反引號,但我不知道如何在這里寫它),但請注意,除了極少數情況,你不能使用非參數化類型Tree<> C#。 並且,如果不將Tree<T>定義為某事,則不能使用Tree<T> 在定義之后, Tree<T1>Tree<T2>將被視為兩個獨立的類層次結構,除非T1 == T2或除非您使用一些in/out方差說明符(如IEnumerable<out> ,因此IEnumerable<Kangaroo> 可以轉換為IEnumerable<object> )。 我假設你想要一個混合方差,所以不是一個選擇。 但是如果您希望樹只是輸入或僅輸出,請立即使用co / contravariance說明符!

所以,讓我們留下仿制品及其差異。 為了提供一個可以理解的C#語言/編譯器的公共基礎,你必須介紹一些更基本的東西。

除了object之外的常見abstract基礎,一些常見的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<IMyThing> 由於顯式接口實現,我不會看到任何“無類型”成員,除非我實際投射它,所以其他T知曉的地方將像往常一樣。

但是,當我必須混合類型時,我現在可以將它們轉換為常見的無類型接口,並且由於接口,我仍然可以訪問操作/屬性。 假設我將以某種方式保證args可以轉換為正確的T.但是我總是可以檢查TypeParam並手動處理它。

所以,純粹的滋擾。 感覺不對,請注意IEnumerable<T>IEnumerable完全一樣!

呼吁dynamic ,但實際上,沒有動態(即.Net 3.5平台沒有DLR?),其他解決方案的空間很小。

另一種方法是使用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() 
};

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM