public class ClaimPath : IEquatable
{
public IEnumerable Identifiers { get; private set; } = new List();
public bool Starred { get; private set; }
public ClaimPath(IEnumerable identifiers, bool starred)
{
// Irrelevant
}
public ClaimPath(string value)
{
// Irrelevant
}
public override bool Equals(object obj)
{
return Equals(obj as ClaimPath);
}
public bool Equals(ClaimPath other)
{
if (other == null)
return false;
return Identifiers.SequenceEqual(other.Identifiers);
}
public override int GetHashCode()
{
return HashCode.Combine(Identifiers);
}
public override string ToString()
{
var path = string.Join("/", Identifiers.Select(v => v.Value));
return Starred ? $"{path}/*" : path;
}
}
public class ClaimPathComparer : IEqualityComparer
{
public ClaimPathComparer()
{
var x = 0; // DEBUGGER HERE ACTIVATES
}
public bool Equals(ClaimPath claimPath1, ClaimPath claimPath2)
{
if (claimPath1 == null || claimPath2 == null)
return false;
return claimPath1.Equals(claimPath2);
}
public int GetHashCode([DisallowNull] ClaimPath obj)
{
return obj.GetHashCode();
}
}
< /code>
Для этого класса у меня есть тест, чтобы конкретно проверить, что различный метод в списке, содержащий объекты этого типа. < /p>
[TestMethod]
public void TheTest()
{
var listOfClaimPaths = new List
{
new ClaimPath("A"),
new ClaimPath("B"),
new ClaimPath("C"),
new ClaimPath("A"),
new ClaimPath("B"),
new ClaimPath("C")
};
var distinctList = listOfClaimPaths.Distinct();
var distinctList2 = listOfClaimPaths.Distinct(new ClaimPathComparer());
Assert.IsFalse(true);
}
Редактировать: удаленная проблема с различным отладчиком. Способен правильно вычислить хэш, разбивая отдельную логику. /п>
Я пишу небольшую библиотеку, где у меня есть этот класс: < /p> [code]public class ClaimPath : IEquatable { public IEnumerable Identifiers { get; private set; } = new List();
public bool Starred { get; private set; }
public ClaimPath(IEnumerable identifiers, bool starred) { // Irrelevant }
public ClaimPath(string value) { // Irrelevant }
public override bool Equals(object obj) { return Equals(obj as ClaimPath); }
public bool Equals(ClaimPath other) { if (other == null) return false;
public override int GetHashCode() { return HashCode.Combine(Identifiers); }
public override string ToString() { var path = string.Join("/", Identifiers.Select(v => v.Value)); return Starred ? $"{path}/*" : path; } }
public class ClaimPathComparer : IEqualityComparer { public ClaimPathComparer() { var x = 0; // DEBUGGER HERE ACTIVATES }
public bool Equals(ClaimPath claimPath1, ClaimPath claimPath2) { if (claimPath1 == null || claimPath2 == null) return false;
return claimPath1.Equals(claimPath2); }
public int GetHashCode([DisallowNull] ClaimPath obj) { return obj.GetHashCode(); } } < /code> Для этого класса у меня есть тест, чтобы конкретно проверить, что различный метод в списке, содержащий объекты этого типа. < /p> [TestMethod] public void TheTest() { var listOfClaimPaths = new List { new ClaimPath("A"), new ClaimPath("B"), new ClaimPath("C"), new ClaimPath("A"), new ClaimPath("B"), new ClaimPath("C") };
var distinctList = listOfClaimPaths.Distinct(); var distinctList2 = listOfClaimPaths.Distinct(new ClaimPathComparer());
Assert.IsFalse(true); } [/code] Редактировать: удаленная проблема с различным отладчиком. Способен правильно вычислить хэш, разбивая отдельную логику. /п>