我敢打賭,它正在呼籲您執行GetHashCode
和該實現(或Equals
)正在拋出一個NullReferenceException。 我們真正能夠回答您的問題的唯一方法是讓您包含XElementComparer
的代碼。
我跑了快速測試,其產生這樣的輸出:
的Equals計數= 1; GetHashCode的數量= 6
[Test]
public void X()
{
var list1 = new List<Alpha> {new Alpha {Bravo = 1}, new Alpha {Bravo = 1}, new Alpha {Bravo = 2}};
var list2 = new List<Alpha> { new Alpha { Bravo = 1 }, new Alpha { Bravo = 3 }, new Alpha { Bravo = 5 } };
var alphaComparer = new AlphaComparer();
Assert.AreEqual(1, list1.Intersect(list2, alphaComparer).Count());
Console.WriteLine("Equals count = {0}; GetHashCode count = {1}", alphaComparer.EqualsCallCount, alphaComparer.GetHashCodeCallCount);
}
class Alpha
{
public int Bravo { get; set; }
}
class AlphaComparer : IEqualityComparer<Alpha>
{
public int EqualsCallCount { get; private set; }
public int GetHashCodeCallCount { get; private set; }
public bool Equals(Alpha x, Alpha y)
{
EqualsCallCount += 1;
return x.Bravo.Equals(y.Bravo);
}
public int GetHashCode(Alpha obj)
{
GetHashCodeCallCount += 1;
return obj.Bravo.GetHashCode();
}
這將有助於添加的'XElementComparer' –
實現你怎麼知道它不是調用'GetHashCode'?這聽起來好像根本就沒有工作...... – Servy
你如何「檢查resultList的內容」?你可以在這樣做的例外情況下提供堆棧跟蹤嗎? – Iridium