I have two similar classes: Person , PersonDto
public class Person { public string Name { get; set; } public long Serial { get; set; } public DateTime Date1 { get; set; } public DateTime? Date2 { get; set; } }
&
public class PersonDto { public string Name { get; set; } public long Serial { get; set; } public DateTime Date1 { get; set; } public DateTime? Date2 { get; set; } }
I have two objects of the same value.
var person = new Person { Name = null , Serial = 123, Date1 = DateTime.Now.Date, Date2 = DateTime.Now.Date }; var dto = new PersonDto { Name = "AAA", Serial = 123, Date1 = DateTime.Now.Date, Date2 = DateTime.Now.Date };
I need to check the value of all properties in two classes by reflection. My ultimate goal is the value of the difference of these properties.
IList diffProperties = new ArrayList(); foreach (var item in person.GetType().GetProperties()) { if (item.GetValue(person, null) != dto.GetType().GetProperty(item.Name).GetValue(dto, null)) diffProperties.Add(item); }
I did this, but the result is not satisfactory. The diffProperties graph for the result was 4 , but the number of expectations was 1 .
Of course, all properties can have null values.
I need a general solution. What should I do?
source share