[英]How to forcibly call interface implemented method?
Considering: EqualityComparer.Default Property, its written in MSDN that: 考虑到:EqualityComparer.Default属性,用MSDN编写,它具有:
The Default property checks whether type T implements the System.IEquatable interface and, if so, returns an EqualityComparer that uses that implementation.
Default属性检查类型T是否实现System.IEquatable接口,如果是,则返回使用该实现的EqualityComparer。 Otherwise, it returns an EqualityComparer that uses the overrides of Object.Equals and Object.GetHashCode provided by T.
否则,它返回一个EqualityComparer,它使用T提供的Object.Equals和Object.GetHashCode的替代。
My Understanding: As it will return EqualityComparer "object", So it should call overrides of Object.Equals and Object.GetHashCode provided by T(if it overrides or Object.Equals and Object.GetHashCode, otherwise). 我的理解:因为它将返回EqualityComparer“ object”,所以它应该调用T提供的Object.Equals和Object.GetHashCode的重写(如果它重写或Object.Equals和Object.GetHashCode,否则)。 How could it call implemented method of IEquatable?
如何称其为IEquatable的实现方法? Is it doing it forcibly?
它是在强行吗? If yes, HOW?
如果是,如何? Correct me if i am wrong.
如果我错了请纠正我。
Here's the MSDN Link: http://msdn.microsoft.com/en-us/library/ms224763.aspx 这是MSDN链接: http : //msdn.microsoft.com/zh-cn/library/ms224763.aspx
Firstly, in a generic class as follows, actually the right method ( IEquatable<T>
) would be selected anyway: 首先,在如下的通用类中,实际上无论如何都会选择正确的方法(
IEquatable<T>
):
private class MyEqualityComparer<T> : IEqualityComparer<T> where T : IEquatable<T>
{
public bool Equals(T x, T y)
{
return x.Equals(y);
}
// Hashcode...
}
Whereas if you cast y to object
, the reference would be to object.Equals
. 而如果将y投射到
object
,则引用将是object.Equals
。 This is just an overloaded method, and so that is resolved at compile time. 这只是一个重载方法,因此可以在编译时解决。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.