class TestData {
Dictionary<int, Dictionary<int, string>> data5 = new Dictionary<int,Dictionary<int, string>> {
{ 1, new Dictionary<int, string> { {10, "aiueo" } } },
{ 2, new Dictionary<int, string> { {20, "aaaaa" } } },
{ 3, new Dictionary<int, string> { {30, "10000" } } },
};
Dictionary<int, int> data4 = new Dictionary<int,int> {
{ 1, 1},
{ 2, 20 },
{ 3, 300 },
};
List<List<int>> data3 = new List<List<int>> {
new List<int> { 1, 2, 3 },
new List<int> { 4, 5, 6 },
};
List<int> data2 = new List<int> {
1, 2, 3
};
int data1 = 100;
}
class Test {
public static List<string> str_list = new List<string>();
public static void run(object obj) {
var type = obj.GetType();
var fields = type.GetFields(BindingFlags.Instance | BindingFlags.NonPublic);
foreach (var f in fields) {
Console.WriteLine(value(obj, f));
}
}
public static string value(object obj, FieldInfo info) {
str_list.Clear();
var v = info.GetValue(obj);
if (v == null) { v = "null"; }
GetValue(info.Name, v);
return string.Join("\r\n", str_list.ToArray());
}
public static void GetValue(string name, object v) {
if (IsList(v)) {
int count = 0;
var list = (IList)v;
foreach (var vv in list){
GetValue(name + "[" + count + "]", vv);
count++;
}
} else if (IsDictionary(v)) {
var dic = (IDictionary)v;
foreach (var key in dic.Keys) {
GetValue(name + "[" + key + "]", dic[key]);
}
} else {
str_list.Add(name + " : " + v);
}
}
public static bool IsList(object obj) {
return obj.GetType().Name.IndexOf("List") != -1;
}
public static bool IsDictionary(object obj) {
return obj.GetType().Name.IndexOf("Dictionary") != -1;
}
}
var data = new TestData();
Test.run(data);
結果
参考
https://oshiete.goo.ne.jp/qa/7894808.html