[英]C# Iterate through nested properties using IEnumerator
I have seen examples (and official ones) for IEnumerator on lists and arrays or dicts, but I have a different problem.我已经在列表和数组或字典上看到了 IEnumerator 的示例(和官方示例),但我有一个不同的问题。 I have classes with properties, how may I implement the IEnumerable and IEnumerator in that case?我有带有属性的类,在这种情况下如何实现 IEnumerable 和 IEnumerator?
My properties classes are:我的属性类是:
public class standardMessage
{
public messageProperties message { get; set; }
public messageFlag flag { get; set; }
}
public class messageProperties
{
public string messageSubject { get; set; }
public string messageBody { get; set; }
}
public class messageFlag
{
public Boolean flagImportant { get; set; }
public Boolean flagPersonal { get; set; }
}
And this is the Program:这是程序:
public class Program
{
static void Main(string[] args)
{
standardMessage myMessage = new standardMessage();
myMessage.message = new messageProperties
{
messageSubject = "Greetings",
messageBody = "Happy Weekend"
};
myMessage.flag = new messageFlag
{
flagImportant = false,
flagPersonal = true
};
//how do I iterate through all properties, without knowing how many are there, instead of writing this worm line of code?
Console.WriteLine(myMessage.message.messageSubject.ToString() + "\r\n" + myMessage.message.messageBody.ToString() + "\r\n" + myMessage.flag.flagImportant.ToString() + "\r\n" + myMessage.flag.flagPersonal.ToString());
Console.ReadLine();
}
}
If you want a production-grade way of printing your objects as a formatted string, you need to go and override ToString
in all your classes to return whatever format you want.如果您想要一种将对象打印为格式化字符串的生产级方式,您需要去覆盖所有类中的ToString
以返回您想要的任何格式。
However, if you just want to print the things on screen for debugging or logging purposes, why not JSON?但是,如果您只是想在屏幕上打印内容以进行调试或记录,为什么不使用 JSON?
public static string ToJson(object @object) =>
System.Text.Json.JsonSerializer.Serialize(@object, new JsonSerializerOptions{WriteIndented = true});
Console.WriteLine(ToJson(myMessage));
Prints印刷
{
"message": {
"messageSubject": "Greetings",
"messageBody": "Happy Weekend"
},
"flag": {
"flagImportant": false,
"flagPersonal": true
}
}
Quick and dirty, but quick and working.又快又脏,但又快又好用。
I made a very primitive object to json converter.我为 json 转换器制作了一个非常原始的对象。 I wouldn't use this in production and it's about 30% slower than Newtonsoft but it get's the job done.我不会在生产中使用它,它比 Newtonsoft 慢约 30%,但它完成了工作。
private static string PrintObject(object obj, int depth = 1)
{
var type = obj.GetType();
if (type.IsPrimitive || type == typeof(Decimal) || type == typeof(String))
return "\"" + obj.ToString() + "\"";
var props = type.GetProperties();
string ret = "";
for (var i = 0; i < props.Length; i++)
{
var val = props[i].GetValue(obj);
ret += new string('\t', depth) + "\"" + props[i].Name + "\":" + PrintObject(val, depth + 1);
if (i != props.Length - 1)
ret += "," + Environment.NewLine;
}
return ("{" + Environment.NewLine + ret + Environment.NewLine + new string('\t', depth - 1) + "}").Replace("\t", " ");
}
Gives the result给出结果
{
"message":{
"messageSubject":"Greetings",
"messageBody":"Happy Weekend"
},
"flag":{
"flagImportant":"False",
"flagPersonal":"True"
}
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.