简体   繁体   English

Assert.Equals实现IEquatable(T)的两个对象不使用equals方法

[英]Assert.Equals of Two objects implementing IEquatable(T) doesn't use the equals method

I have a custom type Type that implement IEquatable(Type). 我有一个实现IEquatable(Type)的自定义类型Type。 Then I new up two instances of the type, none of them are Null 然后我新建了两个类型的实例,它们都不是Null

Assert.IsTrue(obj1.equals(obj2)) //Success
Assert.AreEqual(obj1, obj2) //False
Assert.AreEqual(Type)(obj1, obj2) //False

The first one hits my equals, the second one hits the ToString() Any suggestions? 第一个命中我的等号,第二个击中ToString()任何建议?

update 更新
some code to illustrate: http://pastebin.com/1uecrfeW 一些代码来说明: http//pastebin.com/1uecrfeW

more update 更多更新
If I have to override the base equals, even if a better (generic) equals is available, then what's the use of implementing IEquals(T)? 如果我必须覆盖基数等于,即使有更好的(通用)等于,那么实现IEquals(T)的用途是什么?

My guess is that it's actually hitting Equals(object) instead of Equals(T) . 我的猜测是它实际上击中了Equals(object)而不是Equals(T) If you haven't overridden Equals(object) then it's probably failing the assertion, which then uses ToString to create a useful failure message. 如果你还没有重写Equals(object)那么它可能会使断言失败,然后使用ToString创建一个有用的失败消息。

If you could show a short but complete program which demonstrates the problem (including which Assert method you're calling - NUnit? Something else?) that would help. 如果你能展示一个简短而完整的程序来演示问题(包括你正在调用的Assert方法 - NUnit?还有其他什么?)这会有所帮助。

IIRC Assert.AreEqual is non-generic, so only object.Equals applies; IIRC Assert.AreEqual是非泛型的,因此只有object.Equals适用; try checking the override of non-generic object.Equals. 尝试检查非泛型object.Equals的覆盖。

In addition to the inconvenience of calling a generic method via reflection, the objects could also implement multiple IEquatable<T> (for different T). 除了通过反射调用泛型方法的不便之外,对象还可以实现多个 IEquatable<T> (针对不同的T)。 So the non-generic version makes sense here, IMO. 所以非通用版本在这里有意义,IMO。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM