c# - 将继承接口的类与接口引用进行比较
问题描述
在我的课堂ClassA : ISomeInterface
上,我有:
public class ClassA : ISomeInterface {
Entity entity;
public void Test(){
if(entity.Target == this){}
}
}
定义Target
为entity
:
public class Entity {
public ISomeInterface Target {get; private set;}
}
然后我的编辑给了我一个警告:
Possible unintended reference comparison; to get a value comparison cast the LHS to type `Object`
虽然它可以编译,但这是我第一次收到此警告。我不应该像这样比较接口引用吗?
解决方案
正如您已经说过的,这是警告而不是错误。警告告诉你编译器真正做了什么,也许你故意猜测应该发生的事情是错误的。
话虽如此,让我们进一步挖掘问题可能是什么。这是一些示例代码(请给出完整的示例):
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"
}
}
如您所见,代码实现了相等比较器和Equals()
. 所有这些方法都说所有实例都是相同的,但调用if(Entity == this)
仍然表明它们是不同的。
这就是警告的含义,根本原因是,接口的相等性检查总是回退到 type 中的实现object
。而那个实现是ReferenceEquals(x, y)
.
因此,为避免此警告并明确您的意图,您应该显式编写if(ReferenceEquals(Entity, this))
或实现一个独立的类,该类实现IEqualityComparer<ISomeInterface>
并使用它的实例if(new MyComparer().Equals(Enity, this))
(不要在真实代码中的 if 语句中实例化比较器)来做出比较的意图明显的。
推荐阅读
- python-3.x - 作为函数参数的函数不起作用
- apache-flink - AWS EMR 任务节点上的 Flink
- php - 如何过滤表中的值以在下拉控件中使用它们?
- python - 在搅拌机中渲染不会对相机和光线方向进行旋转更新
- java - 如何使用 Micronaut 在 mongoDB 中创建 TextFree 搜索
- image - 当我们打开相机或图库以在 react native 中获取照片时,应用程序会重新启动
- wordpress - LetsEncrypt 没有 DNS 验证?
- python - 尝试在 Python 中使用 PySimpleGUI 通过 openpyxl 读取 excel 单元格
- model - 如何从持久存储构建域模型?#DDD
- python - 我刚刚开始学习使用 selenium 进行网络抓取。我需要登录本地网站并双击某些项目