Search code examples
c#unit-testingserializationreflectionreflector

Visual Studio / Reflection / Serialization: Can I export values from a debug session to use them for unit testing?


I had to write many dozens of lines of code (see here: https://dotnetfiddle.net/RiVx2E) to generate a few lines of sample data.

In this specific case I could manually export the output variable (see the whole code on Fiddler) in this way:

new List { 
 { IDMacroTab = 1, IDTab = 1, IDSIot = 2 }
 { IDMacroTab = 1, IDTab = 2, IDSIot 1}
 { IDMacroTab = 1, IDTab = 2, IDSIot = 2}
 { IDMacroTab = 1, IDTab = 2, IDSIot = 3}
 { IDMacroTab = 2, IDTab = 1, IDSIot = 1}
 { IDMacroTab = 2, IDTab = 1, IDSIot = 2 }
 { IDMacroTab = 2, IDTab = 2, IDSIot = 1}
 { IDMacroTab = 2, IDTab = 2, IDSIot = 2}
 { IDMacroTab = 2, IDTab = 2, IDSIot = 3}
 { IDMacroTab = 3, IDTab = 1, IDSIot = 1}
 { IDMacroTab = 3, IDTab = 1, IDSIot = 2}
 { IDMacroTab = 3, IDTab = 2, IDSIot = 1}
 { IDMacroTab = 3, IDTab = 2, IDSIot = 2}
 { IDMacroTab = 3, IDTab = 2, IDSIot = 3}};

Is there any workaround that allows to serialize an object to the c# lines of code required to populate it?


Solution

  • First solution (partial)

    I've found this question which can be pretty useful but just for some kind of object (i.e. lists)

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

    enter image description here

    enter image description here

    Second solution

    And also this plugin ObjectExporter (last update 2017; checked at 2018)

    https://marketplace.visualstudio.com/items?itemName=OmarElabd.ObjectExporter

    enter image description here