簡體   English   中英

為什么有時 2 個對象引用相同但不總是

[英]Why sometimes 2 objects reference the same but not always

遵循最后一個答案: 將平面集合轉換為分層集合的遞歸方法?

我想使用相同的方法 CreateTree 但使用另一個對象而不是層次結構:ItemNode:

public class ItemNode
{
    public string Id { get; set; }
    public Item Item { get; set; }
    public ICollection<ItemNode> Children { get; set; }
}

以及 Item 的定義:

public class Item
{
    public string ID { get; set; }
    public string Name { get; set; }
    public int Level { get; set; }
    public string ParentId { get; set; }
}

這里是帶有 ItemNode 的 CreateTree 方法:

    static List<ItemNode> CreateTreeItems(IEnumerable<ItemNode> nodes)
    {
        Dictionary<string,ItemNode> idToNode = nodes.ToDictionary(n => n.Id, n => n);
        List<ItemNode> roots = new List<ItemNode>();
        ItemNode root = null;

        foreach (var n in nodes)
        {
            if (n.Item.ParentId == null)
            {
                if (root != null)
                {
                    roots.Add(root);
                }
                root = n;
                continue;
            }

            ItemNode parent = idToNode[n.Item.ParentId];
            //if (!idToNode.TryGetValue(n.Item.ParentId, out parent))
            //{
            //  //Parent doesn't exist, orphaned entry
            //}

            parent?.Children.Add(n);

            // RETURNS FALSE WHEREAS IN THE ORIGINAL METHOD IT RETURNS TRUE
            var test = Object.ReferenceEquals(parent, root);
            Debug.WriteLine(test);
        }


        if (root == null)
        {
            //There was no root element
        }

        roots.Add(root);
        return roots;
    }

它不起作用,因為 parent 和 root 沒有引用同一個對象(而在原始方法中,它確實如此)。 我想這與我在 ItemNode 類中添加了一個 Item 屬性有關。 但我不知道如何修復它。

謝謝 !

為什么您希望根節點和父節點引用相等? 每個根節點都可能是父節點,但並非每個父節點都是根節點。

可能存在引用相等的情況,但這在很大程度上取決於nodes集合的排序順序。 實際上,當根節點放置在第一級子節點之前時,您會看到引用相等的情況。

我想你的問題在別處,例如,如果Nodes集合根本沒有根節點,它可能無法工作。

這是示例,請在此處嘗試https://dotnetfiddle.net/4r52xP

using System;
using System.Collections.Generic;
using System.Linq;

public class Hierarchy
{
    public Hierarchy(string iD, string name, int level, string parentID, string topParent)
    {
        ID = iD;
        Name = name;
        Level = level;
        ParentID = parentID;
        Children = new HashSet<Hierarchy>();
    }
    public string ID { get; set; }
    public string Name{ get; set; }
    public int Level { get; set; }
    public string ParentID { get; set; }
    public ICollection<Hierarchy> Children { get; set; }
}

public class Program
{
    static Hierarchy CreateTree(IEnumerable<Hierarchy> Nodes)
    {
        var idToNode = Nodes.ToDictionary(n => n.ID, n => n);

        Hierarchy root = null;
        foreach (var n in Nodes)
        {
            if (n.ParentID == null)
            {
                if (root != null)
                {
                    //there are multiple roots in the data
                }
                root = n;
                continue;
            }

            Hierarchy parent = null;
            if (!idToNode.TryGetValue(n.ParentID, out parent))
            {
                //Parent doesn't exist, orphaned entry
            }

            parent.Children.Add(n);

            Console.WriteLine("ReferenceEquals: {0}", Object.ReferenceEquals(parent, root));
        }

        if (root == null)
        {
            //There was no root element
        }
        return root;
    }

    public static void Main()
    {
        Console.WriteLine("Test #1");

        List<Hierarchy> l = new List<Hierarchy>();
        l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the top of the list
        l.Add(new Hierarchy("12345","child1",2,"295152", null));
        l.Add(new Hierarchy("54321","child2",2,"295152", null));
        l.Add(new Hierarchy("44444","child1a",3,"12345", null));
        l.Add(new Hierarchy("33333","child1b",3,"12345", null));
        l.Add(new Hierarchy("22222","child2a",3,"54321", null));
        l.Add(new Hierarchy("22221","child2b",3,"54321", null));
        l.Add(new Hierarchy("22002","child2c",3,"54321", null));
        l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
        l.Add(new Hierarchy("20101","child2b2",4,"22222", null));       

        CreateTree(l);

        Console.WriteLine("\nTest #2");

        l = new List<Hierarchy>();
        l.Add(new Hierarchy("12345","child1",2,"295152", null));
        l.Add(new Hierarchy("54321","child2",2,"295152", null));
        l.Add(new Hierarchy("44444","child1a",3,"12345", null));
        l.Add(new Hierarchy("33333","child1b",3,"12345", null));
        l.Add(new Hierarchy("22222","child2a",3,"54321", null));
        l.Add(new Hierarchy("22221","child2b",3,"54321", null));
        l.Add(new Hierarchy("22002","child2c",3,"54321", null));
        l.Add(new Hierarchy("20001","child2a2",4,"22222", null));
        l.Add(new Hierarchy("20101","child2b2",4,"22222", null));
        l.Add(new Hierarchy("295152","name1",1,null, null)); // <-root node at the bottom of the list
        CreateTree(l);
    }
}

暫無
暫無

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

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