[英]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. 我不得不编写许多行代码(请参阅此处: https : //dotnetfiddle.net/RiVx2E )以生成几行示例数据。
In this specific case I could manually export the output variable (see the whole code on Fiddler) in this way: 在这种特定情况下,我可以通过这种方式手动导出输出变量(请参阅Fiddler上的整个代码):
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? 有什么解决方法可以将对象序列化为填充对象所需的c#代码行?
Thrid Solution: ObjectDumper.NET Thrid解决方案:ObjectDumper.NET
I might have another solution for you. 我可能会为您提供另一种解决方案。 I was using OmarElabd/ObjectExporter in previous projects with good results but there were problems with some kind of types (eg serializations of DateTime didn't work properly at the time).
我在以前的项目中使用的是OmarElabd / ObjectExporter,但效果很好,但是某些类型存在问题(例如,DateTime的序列化在当时无法正常进行)。 Performance was key too: Some generated objects had a size of 50k lines of code (yeh, don't mention this is too big for a single test data class, I know, but...).
性能也很关键:某些生成的对象的代码行大小为5万行(是的,请不要说这对于单个测试数据类来说太大了,但是...)。
After all, I started writing my own ObjectDumper which you can find published as a NuGet package here. 毕竟,我开始编写自己的ObjectDumper ,您可以在此处找到它作为NuGet程序包发布。 The source code is hosted on github, so feel free to contribute.
源代码托管在github上,请随时贡献。 https://www.nuget.org/packages/ObjectDumper.NET/
https://www.nuget.org/packages/ObjectDumper.NET/
The idea of ObjectDumper.NET is that you can dump basically any C# object back to C# initializer code. ObjectDumper.NET的想法是,您基本上可以将任何 C#对象转储回C#初始化程序代码。 Compared to ObjectExporter (which is a Visual Studio plug-in), ObjectDumper is installed as NuGet Package.
与ObjectExporter(这是Visual Studio插件)相比,ObjectDumper是作为NuGet软件包安装的。 This allows ObjectDumper to be used at runtime - not only at the time of code creation.
这使ObjectDumper可以在运行时使用 -不仅在代码创建时使用。
Example Usage: 用法示例:
[Fact]
public void SerializeObjectsToInitializerCode()
{
// Create C# object
var testObjects = new List<TestObject>
{
new TestObject {IDMacroTab = 1, IDTab = 1, IDSIot = 2},
new TestObject {IDMacroTab = 1, IDTab = 2, IDSIot = 1},
new TestObject {IDMacroTab = 1, IDTab = 2, IDSIot = 2}
};
// Pass it to ObjectDumper, choose DumpStyle.CSharp to generate C# initializer code
var dump = ObjectDumper.Dump(testObjects, DumpStyle.CSharp);
// Print to console, write to file, etc...
_testOutputHelper.WriteLine(dump);
}
I've found this question which can be pretty useful but just for some kind of object (ie lists) 我发现这个问题可能非常有用,但仅适用于某种对象(例如列表)
In Visual Studio when debugging C# code, can I export a List or a Dictionary in xml,csv or text format easily? 在Visual Studio中调试C#代码时,是否可以轻松导出xml,csv或文本格式的列表或字典?
And also this plugin ObjectExporter (last update 2017; checked at 2018) 还有这个插件ObjectExporter(最新更新2017;在2018年检查)
https://marketplace.visualstudio.com/items?itemName=OmarElabd.ObjectExporter https://marketplace.visualstudio.com/items?itemName=OmarElabd.ObjectExporter
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.