简体   繁体   English

如何强制调用接口实现的方法?

[英]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.

相关问题 如何在基类上调用显式实现的接口方法 - How to call an explicitly implemented interface-method on the base class 如何使用Type.InvokeMember来调用显式实现的接口方法? - How to use Type.InvokeMember to call an explicitly implemented interface method? 如何调用在接口中定义并在C#中的类中实现的方法? - How to call a method defined in interface and implemented in class in C#? 如何从类中调用方法,但如何从任何接口实现此方法? - How can i call method from class but this method implemented from any interface? 如何调用已实现的接口函数 - How do I call an implemented interface function 如何调用类中实现的接口的方法? - how to call methods of a interface implemented in a class? 如何访问实现接口的类的其他方法 - How to access additional method of class that implemented interface 如何将接口实现为受保护的方法? - How is it possible to have an interface implemented as a protected method? Visual Studio调用层次结构:如何通过已实现的方法接口查找调用? - Visual Studio Call Hierarchy : How to find calls through an implemented interface to a method? 仅当在 C# 中实现时才在接口实现上调用泛型方法 - Call generic method on interface implementation only if implemented in C#
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM