объяснения, БРАВО!
апечатать любую произвольную переменную в C #, чтобы напечатать все члены?
Я нашел три ответа с той же техникой:
https://stackoverflow.com/a/26181763/2125837 который предлагает сериализацию с Json.NET среди других ответов, иhttps://tech.io/playgrounds/2098/how-to-dump-objects-in-c/using-json-and-yaml-serializers,https://www.codeproject.com/Articles/1194980/How-to-Dump-Object-for-Debugging-Purposes-in-CsharОднако, когда я попробовал это, с помощью следующего кода,
using System;
using System.Collections.Generic;
using Newtonsoft.Json;
public static class Program
{
public static void Main()
{
Test t1 = new Test(1, 2);
{
string json = JsonConvert.SerializeObject(t1, Formatting.Indented);
Console.WriteLine(json);
}
Dump(t1);
// Add 3 objects to a List.
List<Test> list = new List<Test>();
list.Add(new Test(1, 2));
list.Add(new Test(3, 4));
list.Add(new Test(5, 6));
Console.WriteLine(list.ToString());
{
string json = JsonConvert.SerializeObject(list, Formatting.Indented);
Console.WriteLine(json);
}
}
public class Test
{
int A;
int b;
public Test(int _a, int _b)
{
A = _a;
b = _b;
}
};
public static void Dump<T>(this T x)
{
string json = JsonConvert.SerializeObject(x, Formatting.Indented);
Console.WriteLine(json);
}
}
Все, что я получил, пустоTest
выходы:
{}
{}
System.Collections.Generic.List`1[Program+Test]
[
{},
{},
{}
]
Почему все мои ученики отсутствуют при сериализации в JSON с Json.NET?