Ich habe eine Liste von einem Objekt List<IReportRelationMapping>
, die ich überprüfen müssen, wenn die Liste keine spezifische ReportRelationMapping
ObjektWie überschreiben Enthält die Eigenschaft einer Liste in C#?
Hier enthält, ist, wie meine ReportRelationMapping
wie
public class ReportRelationMapping : IReportRelationMapping
{
public string Name { get; set; }
public IReportRelation LocalRelation { get; set; }
public IReportRelation ForeignRelation { get; set; }
public IReportRelationMapping RelatedThrough { get; set; }
}
Die Liste sieht ein Objekt enthält, wenn this.LocalRelation == passed.LocalRelation && this.ForeignRelation == passed.ForeignRelation
oder this.LocalRelation == passed.ForeignRelation && this.ForeignRelation == passed.LocalRelation
Hier ist, was ich getan, um haben die Contains
Eigenschaft der Liste
public class ReportRelationMapping : IReportRelationMapping
{
public string Name { get; set; }
public IReportRelation LocalRelation { get; set; }
public IReportRelation ForeignRelation { get; set; }
public IReportRelationMapping RelatedThrough { get; set; }
public bool Equals(ReportRelationMapping other)
{
if (other == null)
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
if (this.GetType() != other.GetType())
{
return false;
}
return (this.LocalRelation == other.LocalRelation && this.ForeignRelation == other.ForeignRelation)
|| (this.LocalRelation == other.ForeignRelation && this.ForeignRelation == other.LocalRelation);
}
public override bool Equals(object other)
{
if (other == null)
{
return false;
}
if (object.ReferenceEquals(this, other))
{
return true;
}
if (this.GetType() != other.GetType())
{
return false;
}
return this.Equals(other as ReportRelationMapping);
}
public override int GetHashCode()
{
int hash = 14;
hash = (hash * 7) + this.ForeignRelation.GetHashCode();
return (hash * 7) + this.LocalRelation.GetHashCode();
}
public static bool operator ==(ReportRelationMapping lhs, ReportRelationMapping rhs)
{
// Check for null on left side.
if (Object.ReferenceEquals(lhs, null))
{
if (Object.ReferenceEquals(rhs, null))
{
// null == null = true.
return true;
}
// Only the left side is null.
return false;
}
// Equals handles case of null on right side.
return lhs.Equals(rhs);
}
public static bool operator !=(ReportRelationMapping lhs, ReportRelationMapping rhs)
{
return !(lhs == rhs);
}
}
Aber aus irgendeinem Grund, auch wenn die Liste ein gebendes Objekt enthält, bekomme ich false
oder "enthält nicht das Objekt." Wenn ich meinen Code debugge, kann ich sehen, dass der Debugger die Equal
Methode erreicht, so dass es meinen Code durchläuft, aber es erreicht nie GetHashCode
Methode. Ich bin mir nicht sicher, ob ich meine GetHashCode
Methode hier falsch implementiere.
Was habe ich hier vermisst? Warum enthält in meinem Fall immer "Nicht enthalten"? Wie kann ich diese Contains
Methode einer Liste korrekt überschreiben?
Nun Ihre Hash-Code mit den Equals nicht übereinstimmt, wie Es sollte dasselbe für zwei gleiche Objekte zurückgeben. Der Hash sollte etwas mehr sein als 'return this.ForeignRelation.GetHashCode() + this.LocalRelation.GetHashCode();' – juharr