I have the following classes:
public class Person { public String FirstName { set; get; } public String LastName { set; get; } public Role Role { set; get; } } public class Role { public String Description { set; get; } public Double Salary { set; get; } public Boolean HasBonus { set; get; } }
I want to be able to automatically extract the property value diferences between Person1 and Person2, example as below:
public static List<String> DiffObjectsProperties(T a, T b) { List<String> differences = new List<String>(); foreach (var p in a.GetType().GetProperties()) { var v1 = p.GetValue(a, null); var v2 = b.GetType().GetProperty(p.Name).GetValue(b, null); /* What happens if property type is a class e.g. Role??? * How do we extract property values of Role? * Need to come up a better way than using .Namespace != 'System' */ if (!v1.GetType() .Namespace .Equals('System', StringComparison.OrdinalIgnoreCase)) continue; //add values to differences List } return differences; }
How can I extract property values of Role in Person???
1 Answer