动态创建嵌套对象

时间:2014-10-29 07:42:49

标签: c#

我正在使用第三方库,如果我想创建目录的嵌套结构,我必须像这样创建

new ClassA("folder1", new ClassA("folder2", new ClassA("folder3")));

这将创建文件夹结构,如此folder1-> folder2-> folder3。

为了让我的用户更简单,我正在创建方法,用户将路径作为参数传递,我的方法处理路径,并创建上面的对象结构,在内部创建文件夹结构。

现在我能够像树一样解析路径,但无法创建上面的对象结构。 这是示例控制台应用程序代码

 class Program
{
    static void Main(string[] args)
    {
        List<string> Paths = new List<string>();
        Paths.Add("D1");
        Paths.Add("E1");
        Paths.Add(@"E1\E11");
        Paths.Add(@"D1\D11");
        Paths.Add(@"D1\D12");
        Paths.Add(@"D1\D2");
        Paths.Add(@"D1\D2\D21");

        Node nodeObj = new Node();

        foreach (var path in Paths)
        {
            nodeObj.AddPath(path);
        }

        //var nodes = nodeObj.Nodes;
        Node current = nodeObj;
        int level = 0;
        ReadNodes(current, level);

     }

    private static void ReadNodes(Node current, int level)
    {
        foreach (string key in current.Nodes.Keys)
        {
            var tablevel = level;
            string tab = string.Empty;
            while (tablevel>0)
            {
                tab = tab + "  ";
                tablevel--;
            }
            Console.WriteLine(tab +":" + key);

            // The child node.
            Node child;

            if (current.Nodes.TryGetValue(key, out child) && child.Nodes.Count>0)
            {
                ReadNodes(child, level+1);
            }
             else { }

        }
    }
}

public class Node
{
    private readonly IDictionary<string, Node> _nodes =
        new Dictionary<string, Node>();

    public IDictionary<string, Node> Nodes
    {
        get { return _nodes; }
    }

    private string Path { get; set; }

    public string Source { get; set; }

    public void AddPath(string path)
    {
        char[] charSeparators = new char[] { '\\' };

        // Parse into a sequence of parts.
        string[] parts = path.Split(charSeparators,
            StringSplitOptions.RemoveEmptyEntries);

        // The current node.  Start with this.
        Node current = this;

        // Iterate through the parts.
        foreach (string part in parts)
        {
            // The child node.
            Node child;

            // Does the part exist in the current node?  If
            // not, then add.
            if (!current._nodes.TryGetValue(part, out child))
            {
                // Add the child.
                child = new Node
                {
                    Path = part
                };

                // Add to the dictionary.
                current._nodes[part] = child;
            }

            // Set the current to the child.
            current = child;

        }
    }
}

1 个答案:

答案 0 :(得分:0)

class Test : yourClass
{
    string name;
    Test childTest;

    public Test(string name)
    {
        this.name = name;
        this.childTest = null;
    }
    public Test(string name, Test test)
    {
        this.name = name;
        this.childTest = test;
    }
}

Test a = new Test("a", new Test("b", new Test("c")));

你只是在寻找这样的结构吗?你需要两个构造函数,一个只接受一个字符串,一个接受一个字符串,另一个接受同一类型的