简体   繁体   English

将继承接口的 class 与接口引用进行比较

[英]Comparing a class that inherits an interface to an interface reference

In my class ClassA: ISomeInterface i have:在我的 class ClassA: ISomeInterface我有:

public class ClassA : ISomeInterface {
    Entity entity;
    public void Test(){
       if(entity.Target == this){}
    }    
}

With Target in entity defined as: entity中的Target定义为:

public class Entity {     

   public ISomeInterface Target {get; private set;}    

}

My editor then gives me a warning:然后我的编辑给了我一个警告:

Possible unintended reference comparison; to get a value comparison cast the LHS to type `Object`

在此处输入图像描述

Although it compiles, this is the first time I've had this warning.虽然它可以编译,但这是我第一次收到此警告。 Should I not be comparing interface references like this?我不应该像这样比较接口引用吗?

As you already stated it is a warning not an error.正如您已经说过的,这是警告而不是错误。 And the warning tells you what the compiler really does and maybe your intentionnally guess what should happen is wrong.警告告诉你编译器真正做了什么,也许你故意猜测应该发生的事情是错误的。

So being said that, lets further dig what the problem could be.话虽如此,让我们进一步挖掘问题可能是什么。 Here is some sample code (that what's being meant with please give an complete example ):这是一些示例代码(请给出完整的示例):

using System;

public interface ISomeInterface
{
}

public class ClassA : ISomeInterface
{
    public ClassA(int id)
    {
        Id = id;
    }

    public int Id { get; }

    public ISomeInterface Entity { get; set; }
    
    public static bool operator== (ClassA x, ClassA y)
    {
        return true;
    }
    
    public static bool operator!= (ClassA x, ClassA y)
    {
        return false;
    }

    public override bool Equals(object obj)
    {
        return true;
    }
    
    public override int GetHashCode()
    {
        return 72;
    }

    public void Test()
    {
        if (Entity == this)
        {
            Console.WriteLine("same");
        }
        else
        {
            Console.WriteLine("different");
        }
    }
}

public class Program
{
    public static void Main()
    {
        var first = new ClassA(1);
        var second = new ClassA(1);
        first.Entity = second;
        first.Test(); // writes "different"
    }
}

As you can see the code implements the equality comparers and Equals() .如您所见,代码实现了相等比较器和Equals() All these methods are saying that all instance are the same, but the call in if(Entity == this) still tells that they are different.所有这些方法都说所有实例都是相同的,但是if(Entity == this)中的调用仍然表明它们是不同的。

This is the meaning of the warning and the root cause is, that an equality check of an interface always falls back to the implementation within the type object .这是警告的含义,根本原因是接口的相等性检查总是回object类型内的实现。 And that implementation is ReferenceEquals(x, y) .该实现是ReferenceEquals(x, y)

So to avoid this warning and make your intention clear you should either explicitly write if(ReferenceEquals(Entity, this)) or implement a stand-alone class that implements IEqualityComparer<ISomeInterface> and use an instance of it if(new MyComparer().Equals(Enity, this)) (do not instantiate the comparer within the if statement in real code) to make your intentation of comparison obvious.因此,为避免此警告并明确您的意图,您应该明确编写if(ReferenceEquals(Entity, this))或实现一个独立的 class 实现IEqualityComparer<ISomeInterface>并使用它的实例if(new MyComparer().Equals(Enity, this)) (不要在实际代码中的 if 语句中实例化比较器)以使您的比较意图显而易见。

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

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