I have these data transfer objects:
public class Report
{
public int Id { get; set; }
public int ProjectId { get; set; }
//and so on for many, many properties.
}
I don’t want to write
public bool areEqual(Report a, Report b)
{
if (a.Id != b.Id) return false;
if (a.ProjectId != b.ProjectId) return false;
//Repeat ad nauseum
return true;
}
Is there a faster way to test if two object with only properties have the same values (something that doesn’t require one line of code or one logical expression per property?)
Switching to structs is not an option.
How about some reflection, perhaps using
Expression.Compile()for performance? (note the static ctor here ensures we only compile it once perT):Edit: updated to handle fields too: