私はresharperを使用して平等メンバーを生成してきました。これは、単体テストに非常に役立ちました。
ただし、オブジェクトにリストが含まれていると、うまく機能しないようです。
public class FileandVersions
{
public string fileName { get; set; }
public string assetConfigurationType { get; set; }
public List<Versions> Versions { get; set; }
protected bool Equals(FileandVersions other)
{
return string.Equals(fileName, other.fileName) && string.Equals(assetConfigurationType, other.assetConfigurationType) && Equals(Versions, other.Versions);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((FileandVersions) obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = (fileName != null ? fileName.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (assetConfigurationType != null ? assetConfigurationType.GetHashCode() : 0);
hashCode = (hashCode*397) ^ (Versions != null ? Versions.GetHashCode() : 0);
return hashCode;
}
}
}
これがバージョンオブジェクトの定義です。
public class Versions
{
public string versionNumber { get; set; }
public DateTime acctivationTime { get; set; }
public string URL { get; set; }
protected bool Equals(Versions other)
{
return string.Equals(versionNumber, other.versionNumber) && acctivationTime.Equals(other.acctivationTime) && string.Equals(URL, other.URL);
}
public override bool Equals(object obj)
{
if (ReferenceEquals(null, obj)) return false;
if (ReferenceEquals(this, obj)) return true;
if (obj.GetType() != this.GetType()) return false;
return Equals((Versions) obj);
}
public override int GetHashCode()
{
unchecked
{
int hashCode = (versionNumber != null ? versionNumber.GetHashCode() : 0);
hashCode = (hashCode*397) ^ acctivationTime.GetHashCode();
hashCode = (hashCode*397) ^ (URL != null ? URL.GetHashCode() : 0);
return hashCode;
}
}
}
オブジェクトが同等であっても、これらのオブジェクトの比較は失敗します。オブジェクトにリストが含まれている場合に等価メンバーを書き込むための最良の方法は何ですか?