In Visual Studio when debugging C# code, can I export a List or a Dictionary in xml,csv or text format easily?

后端 未结 4 1367
执笔经年
执笔经年 2021-01-17 07:19

In Visual Studio when debugging C# code, can I export a Dictionary in xml,csv or text format easily?

I would like to export a

4条回答
  •  长发绾君心
    2021-01-17 08:24

    Use The Immediate Window

    Don't underestimate the power of the immediate window for this kind of thing.

    My advice is to add an extension method to Dictionary which creates your csv file, then, in the Immediate window, you can call it whenever and wherever you need.

    public static class DictionaryExtensions
    {
        public static string ToCsvFormat(this IDictionary dict)
        {
            var sw = new StringWriter();
            foreach(var kv in dict)
            {
                sw.WriteLine($"{kv.Key}, {kv.Value}");
            }
            return sw.ToString();
        }
    }
    

    Here's a C# fiddle of the extension method in action:

    https://dotnetfiddle.net/f8xQjs

    And to use it in the immediate window:

    foo.ToCsvFormat(),nq
    

    the ",nq" option causes it to properly handle multi-lines. Then you can copy/paste from the immediate window and you're good to go.

提交回复
热议问题