递归地打印收藏集

时间:2019-04-17 17:06:44

标签: c#

我已经看到很多与此有关的问题,但是我没有看到关于递归执行此问题的任何信息。我创建了一些扩展方法,这些方法可以很好地完成深度为1的漂亮打印。它看起来像这样:

    public static string PrettyToString<K, V>(this Dictionary<K, V> dictionary)
    {
        string result = "{";
        foreach (var kvp in dictionary)
        {
            result += $"({kvp.Key}, {kvp.Value}) ";
        }
        result += "}";
        return result;
    }

    public static string PrettyToString<T>(this List<T> list)
    {
        string result = "{";
        foreach (var element in list)
        {
            result += $"{element}, ";
        }
        result += "}";
        return result;
    }

    public static string PrettyToString<T>(this T [] list)
    {
        string result = "{";
        foreach (var element in list)
        {
            result += $"{element}, ";
        }
        result += "}";
        return result;
    }

但是,如果KVT是另一个像ListDictionary这样的集合怎么办?我想递归打印漂亮的东西,但是我不确定该怎么做。结果,我的输出看起来像这样:

{(foo, System.Collections.Generic.Dictionary`2[System.String,System.Boolean])...

我希望它看起来像这样:

    {(foo, {(bar, true)})...

我正在寻找无论嵌套类型如何都递归打印的方法:

var x = new List<Dictionary<string, string>>();
var y = new Dictionary<Dictionary<string, string>, string>>();
var z = new Dictionary<Dictionary<string, string>, List<string>>>();
...
x.PrettyToString();
y.PrettyToString();
z.PrettyToString();

应全部递归打印内容。我该如何实现?

1 个答案:

答案 0 :(得分:1)

我更改了方法的签名,并使其非通用。

诀窍是在转换之前确定类型。

查看下面的示例代码。希望对您有所帮助。

请查看源代码底部的 Ext 类。

Try it online

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

namespace ConsoleApp8
{
    class Program
    {
        static void Main(string[] args)
        {
            var Dic = new Dictionary<int, string> { { 1, "Ali" }, { 2, "B" } };
            Console.WriteLine(Dic.PrettyToString());

            var Dic1 = new Dictionary<string, float> { { "Ali", 12.5f }, { "B", 99.9f } };
            Console.WriteLine(Dic1.PrettyToString());

            var Dic2 = new Dictionary<List<int>, string>
            {
                { new List<int> { 1, 2, 3 }, "A" },
                { new List<int> { 4, 5, 6 }, "B" }
            };
            Console.WriteLine(Dic2.PrettyToString());

            var Dic3 = new Dictionary<Dictionary<string, string>, string>
            {
                { new Dictionary<string, string> { { "a", "A" }, { "b", "B" } }, "Capital" },
                { new Dictionary<string, string> { { "1", "1" }, { "2", "4" }, { "4", "16" } }, "Power" }
            };
            Console.WriteLine(Dic3.PrettyToString());

            var Dic4 = new Dictionary<Dictionary<string, string>, List<string>>
            {
                { new Dictionary<string, string> { { "a", "A" }, { "b", "B" } }, new List<string> { "A", "B" } },
                { new Dictionary<string, string> { { "1", "1" }, { "2", "4" }, { "4", "16" } }, new List<string> { "1", "2", "4" } }
            };
            Console.WriteLine(Dic4.PrettyToString());

            var L = new List<List<int>>
            {
                new List<int> { 1, 2, 3 },
                new List<int> { 4, 5, 6 }
            };
            Console.WriteLine(L.PrettyToString());

            Console.ReadKey();
        }
    }

    static class Ext
    {
        public static string PrettyToString(this IDictionary dictionary)
        {
            string result = "{";
            foreach (var Key in dictionary.Keys)
            {
                result += string.Format("({0}, {1}) ", PrettyToString(Key), PrettyToString(dictionary[Key]));
            }
            result += "}";
            return result;
        }

        public static string PrettyToString(this IEnumerable list)
        {
            string result = "{";
            foreach (var element in list)
            {
                result += string.Format("{0}, ", PrettyToString(element));
            }
            result += "}";
            return result;
        }

        private static string PrettyToString(object O)
        {
            var S = O as string;
            if (S != null) return S;

            var D = O as IDictionary;
            if (D != null) return D.PrettyToString();

            var L = O as IEnumerable;
            if (L != null) return L.PrettyToString();

            return O.ToString();
        }
    }
}