C# 使用 IEnumerator 遍历嵌套属性
C# Iterate through nested properties using IEnumerator
我在列表和数组或字典上看到了 IEnumerator 的示例(和官方示例),但我有一个不同的问题。我有 类 属性,在这种情况下我如何实现 IEnumerable 和 IEnumerator?
我的属性 类 是:
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; }
}
这是程序:
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();
}
}
如果您想要一种生产级方式将您的对象打印为格式化字符串,您需要在所有 类 到 return 中覆盖 ToString
任何格式想要。
但是,如果您只想在屏幕上打印内容以进行调试或记录,为什么不 JSON?
public static string ToJson(object @object) =>
System.Text.Json.JsonSerializer.Serialize(@object, new JsonSerializerOptions{WriteIndented = true});
Console.WriteLine(ToJson(myMessage));
版画
{
"message": {
"messageSubject": "Greetings",
"messageBody": "Happy Weekend"
},
"flag": {
"flagImportant": false,
"flagPersonal": true
}
}
快速而肮脏,但又快又好用。
我制作了一个非常原始的对象到 json 转换器。我不会在生产中使用它,它比 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", " ");
}
给出结果
{
"message":{
"messageSubject":"Greetings",
"messageBody":"Happy Weekend"
},
"flag":{
"flagImportant":"False",
"flagPersonal":"True"
}
}
我在列表和数组或字典上看到了 IEnumerator 的示例(和官方示例),但我有一个不同的问题。我有 类 属性,在这种情况下我如何实现 IEnumerable 和 IEnumerator?
我的属性 类 是:
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; }
}
这是程序:
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();
}
}
如果您想要一种生产级方式将您的对象打印为格式化字符串,您需要在所有 类 到 return 中覆盖 ToString
任何格式想要。
但是,如果您只想在屏幕上打印内容以进行调试或记录,为什么不 JSON?
public static string ToJson(object @object) =>
System.Text.Json.JsonSerializer.Serialize(@object, new JsonSerializerOptions{WriteIndented = true});
Console.WriteLine(ToJson(myMessage));
版画
{
"message": {
"messageSubject": "Greetings",
"messageBody": "Happy Weekend"
},
"flag": {
"flagImportant": false,
"flagPersonal": true
}
}
快速而肮脏,但又快又好用。
我制作了一个非常原始的对象到 json 转换器。我不会在生产中使用它,它比 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", " ");
}
给出结果
{
"message":{
"messageSubject":"Greetings",
"messageBody":"Happy Weekend"
},
"flag":{
"flagImportant":"False",
"flagPersonal":"True"
}
}