如何检查所有两个对象的属性是否相等,包括派生属性?
How to check whether all two objects' properties are equal, including derived ones?
假设我有这三个 classes:
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int IdNumber { get; set; }
public string Address { get; set; }
// Constructor and methods.
}
class Employee : Person
{
public byte SalaryPerHour { get; set; }
public byte HoursPerMonth { get; set; }
// Constructor and methods.
}
class Seller : Employee
{
public short SalesGoal { get; set; }
public bool MetSaleGoleLastYear { get; set; }
// Constructor and methods.
}
我会这样实现 IEquatable<T>
:
public bool Equals(Person other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address;
}
public bool Equals(Employee other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address
&& SalaryPerHour == other.SalaryPerHour
&& HoursPerMonth == other.HoursPerMonth;
}
public bool Equals(Seller other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address
&& SalaryPerHour == other.SalaryPerHour
&& HoursPerMonth == other.HoursPerMonth
&& SalesGoal == other.SalesGoal
&& MetSaleGoleLastYear == other.MetSaleGoleLastYear;
}
现在,如您所见,class 在继承链中越靠下,我需要检查的属性就越多。例如,如果我继承了别人编写的 class,我还需要查看 class 代码来找到它的所有属性,这样我就可以使用它们来检查值是否相等。这对我来说听起来很奇怪。没有更好的方法吗?
使用基地。短得多。
public bool Equals(Seller other)
{
if (other == null) return false;
return base.Equals(other)
&& SalesGoal == other.SalaryPerHour;
&& MetSaleGoleLastYear == other.HoursPerMonth;
}
除了 John Wu 的回答之外,这里是更正的完整解决方案:
public bool Equals(Person other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address;
}
public bool Equals(Employee other)
{
if (other == null) return false;
return base.Equals(other)
&& SalaryPerHour == other.SalaryPerHour // ; removed
&& HoursPerMonth == other.HoursPerMonth;
}
public bool Equals(Seller other)
{
if (other == null) return false;
return base.Equals(other)
&& SalesGoal == other.SalesGoal // SalaryPerHour;
&& MetSaleGoleLastYear == other.MetSaleGoleLastYear; //HoursPerMonth;
}
假设我有这三个 classes:
class Person
{
public string FirstName { get; set; }
public string LastName { get; set; }
public int IdNumber { get; set; }
public string Address { get; set; }
// Constructor and methods.
}
class Employee : Person
{
public byte SalaryPerHour { get; set; }
public byte HoursPerMonth { get; set; }
// Constructor and methods.
}
class Seller : Employee
{
public short SalesGoal { get; set; }
public bool MetSaleGoleLastYear { get; set; }
// Constructor and methods.
}
我会这样实现 IEquatable<T>
:
public bool Equals(Person other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address;
}
public bool Equals(Employee other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address
&& SalaryPerHour == other.SalaryPerHour
&& HoursPerMonth == other.HoursPerMonth;
}
public bool Equals(Seller other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address
&& SalaryPerHour == other.SalaryPerHour
&& HoursPerMonth == other.HoursPerMonth
&& SalesGoal == other.SalesGoal
&& MetSaleGoleLastYear == other.MetSaleGoleLastYear;
}
现在,如您所见,class 在继承链中越靠下,我需要检查的属性就越多。例如,如果我继承了别人编写的 class,我还需要查看 class 代码来找到它的所有属性,这样我就可以使用它们来检查值是否相等。这对我来说听起来很奇怪。没有更好的方法吗?
使用基地。短得多。
public bool Equals(Seller other)
{
if (other == null) return false;
return base.Equals(other)
&& SalesGoal == other.SalaryPerHour;
&& MetSaleGoleLastYear == other.HoursPerMonth;
}
除了 John Wu 的回答之外,这里是更正的完整解决方案:
public bool Equals(Person other)
{
if (other == null) return false;
return FirstName == other.FirstName
&& LastName == other.LastName
&& IdNumber == other.IdNumber
&& Address == other.Address;
}
public bool Equals(Employee other)
{
if (other == null) return false;
return base.Equals(other)
&& SalaryPerHour == other.SalaryPerHour // ; removed
&& HoursPerMonth == other.HoursPerMonth;
}
public bool Equals(Seller other)
{
if (other == null) return false;
return base.Equals(other)
&& SalesGoal == other.SalesGoal // SalaryPerHour;
&& MetSaleGoleLastYear == other.MetSaleGoleLastYear; //HoursPerMonth;
}