c# propertyinfo get value
private void ComparePropertyInfos<T>(T obj1, T obj2) {
StringBuilder message1 = new StringBuilder(), message2 = new StringBuilder();
Type t = obj1.GetType();
System.Reflection.PropertyInfo[] props = t.GetProperties();
foreach(System.Reflection.PropertyInfo prop in props) {
string color = "green";
message1.Append($"Name: {prop.Name}, Type: {prop.PropertyType.Name}");
message2.Append($"Name: {prop.Name}, Type: {prop.PropertyType.Name}");
if(prop.GetIndexParameters().Length == 0) {
object value1 = prop.GetValue(obj1);
object value2 = prop.GetValue(obj2);
if(value1 != null && value2 != null) {
color = value1.Equals(value2) ? "green" : "red";
} else {
color = "blue";
}
message1.Append($", Value: <b><color={color}>{value1}</color></b>");
message2.Append($", Value: <b><color={color}>{value2}</color></b>");
}
message1.Append(Environment.NewLine);
message2.Append(Environment.NewLine);
}
Console.WriteLine(message1);
Console.WriteLine(message2);
}