为什么当我调用此哈希集的删除操作时,它既不使用 gethashcode 也不使用相等性实现?

Why when i call the remove operation of this hashset it doesn't use the gethashcode neither the equality implementation?

我正在做一个应用程序来管理角色扮演会话的创建,但是我在做规则总结的部分有问题所以大师不必每秒钟都读核心书,我有数据以这种方式构建。

用户有一个活动列表,那个活动有一个场景列表,那个场景有一个冒险列表。

用户 -> Lcampaings -> Lscenaries -> Ladventures

每个活动、场景或冒险都有包含文档、图像、资源等列表和摘要哈希集的资源。

Campaign/Scenary/Adventure -> 资源 -> Ldocuments/LImages/.../HashSet 摘要

好的,为了修改摘要,我已经实现了平等和 gethashcode

using System;
using System.Collections;
using System.Collections.Generic;
using System.ComponentModel;
using System.Diagnostics.CodeAnalysis;
using System.Text;
using System.Windows;

namespace ElEscribaDelDJ.Classes
{
    public class Resumenes: INotifyPropertyChanged, IEqualityComparer<Resumenes>
    {

        private string _nombre;

        public string Nombre
        {
            get { return _nombre; }
            set { 
                _nombre = value;
                OnPropertyChanged("Nombre");
            }
        }

        private string _etiquetas;

        public string Etiquetas
        {
            get { return _etiquetas; }
            set { 
                _etiquetas = value;
                OnPropertyChanged("Etiquetas");
            }
        }

        private string _descripcion;

        public string Descripcion
        {
            get { return _descripcion; }
            set { 
                _descripcion = value;
                OnPropertyChanged("Descripcion");
            }
        }

        private int _pagina;

        public int Pagina
        {
            get { return _pagina; }
            set {
                if (value <= 0) 
                    _pagina = 1;
                else
                    _pagina = value;
                OnPropertyChanged("Pagina");
            }
        }

        private string _manual;

        public string Manual
        {
            get { return _manual; }
            set { 
                _manual = value;
                OnPropertyChanged("Manual");
            }
        }

        private string _manualurl;

        public string ManualUrl
        {
            get { return _manualurl; }
            set
            {
                _manualurl = value;
                OnPropertyChanged("ManualUrl");
            }
        }

        private string _tipoaventura;

        public string TipoAventura
        {
            get { return _tipoaventura; }
            set { 
                _tipoaventura = value;
                OnPropertyChanged("TipoAventura");
            }
        }

        private string _nombretipoaventura;

        public string NombreTipoAventura
        {
            get { return _nombretipoaventura; }
            set {
                _nombretipoaventura = value;
                OnPropertyChanged("NombreTipoAventura");
            }
        }


        private int _indice;

        public int Indice
        {
            get { return _indice; }
            set
            {
                _indice = value;
                OnPropertyChanged("Indice");
            }
        }

        private List<int> _indiceslibres;

        public List<int> IndicesLibres
        {
            get { return _indiceslibres; }
            set
            {
                _indiceslibres = value;
                OnPropertyChanged("IndicesLibres");
            }
        }

        public event PropertyChangedEventHandler PropertyChanged;

        public void OnPropertyChanged(string PropertyName)
        {
            if (PropertyChanged != null)
                PropertyChanged(this, new PropertyChangedEventArgs(PropertyName));
        }

        public bool Equals(Resumenes x, Resumenes y)
        {
            if (x.Nombre.Equals(y.Nombre) && x.Descripcion.Equals(y.Descripcion))
                return true;
            else
                return false;
        }

        public int GetHashCode(Resumenes obj)
        {
            MessageBox.Show("El Hash code es " + obj.Nombre.GetHashCode());
            return obj.Nombre.GetHashCode();
        }

        public Resumenes CopiarValores ()
        {
            return (Resumenes)this.MemberwiseClone();
        }
    }
}

(在 gethashcode 中我有消息框只是为了知道是否被调用了 ofc 我知道它不应该在那里)

我正在使用两个对象的名称和描述来判断它们是否相等,对于 gethashcode 则使用名称。

我在阅读了很多关于 hashcode 和 equality 的工作原理的问题后做了这个,hashcodeA == hashcodeB 意味着它们可以相等,所以名称看起来很完美,这就是为什么我在 equallity 中也使用描述,因为如果您有相同的名字和相同的描述,那么它的摘要基本相同。

好的,所以我显示了所有摘要的列表,用户 select 一个,单击编辑,在 windows 中添加或编辑我做了一个不在对象的深度复制和之后我调用活动编辑摘要,其中我删除旧对象并添加新对象,因为我读到如果您修改了用于制作哈希码的字段,那是最好的方法。

public void EditarResumen(Resumenes resumenviejo, Resumenes resumennuevo)
        {
            DatosAplicacion.CampanaSeleccionada.Recursos.Resumenes.Remove(resumenviejo);
            DatosAplicacion.CampanaSeleccionada.Recursos.Resumenes.Add(resumennuevo);
            RecursosAplicacion.SesionUsuario.ReemplazarCampana();
        }

“Datosaplicacion”是静态的class,其中包含用户从中选择的所有活动、场景和冒险

using System;
using System.Collections.Generic;
using System.Text;

namespace ElEscribaDelDJ.Classes.Utilidades.Aplicacion
{
    public static class DatosAplicacion
    {
        private static Campana _campana = new Campana();

        public static Campana CampanaSeleccionada
        {
            get { return _campana; }
            set { _campana = value; }
        }

        public static int IndiceCampana;

        private static EscenarioCampana _escenarioseleccionado = new EscenarioCampana();

        public static EscenarioCampana EscenarioSeleccionado
        {
            get { return _escenarioseleccionado; }
            set { _escenarioseleccionado = value; }
        }

        public static int IndiceEscenario;

        private static Aventura _aventuraseleccionada;

        public static Aventura AventuraSeleccionada
        {
            get { return _aventuraseleccionada; }
            set { _aventuraseleccionada = value; }
        }

        public static int IndiceAventuraSeleccionada;
    }
}

resumenviejo(oldsummary)是用

制作的
public Resumenes CopiarValores ()
        {
            return (Resumenes)this.MemberwiseClone();
        }

这应该没问题,因为我没有任何参考对象或类似物。

但是当我调试应用程序时,删除操作总是抛出错误,并且从不调用相等操作,也不调用 gethashcode。

我不知道发生了什么。

我用这篇文章做了操作https://dotnetcodr.com/2017/01/12/how-to-check-whether-two-hashsets-are-equal-in-c-net-2/#:~:text=Two%20HashSet%20objects%20in%20C#,their%20order%20in%20the%20collection

我已将完整代码上传至 github https://github.com/davidgmd/Proyecto-de-fin-de-grado

你有两种方法GetHashCodeEquals

public bool Equals(Resumenes x, Resumenes y)

public int GetHashCode(Resumenes obj)

但是它们并没有覆盖框架中的正确方法,所以它们不会被调用。您必须重写以下方法,以便框架使用它们

public override bool Equals(object obj) {
  if (!(obj is Resumenes)) return false;
  var other = obj as Resumenes;
  return this.Nombre.Equals(other.Nombre) && this.Descripcion.Equals(other.Descripcion);
}

public override int GetHashCode() {
   return this.Nombre.GetHashCode();
}

请注意,this 并不是真正需要的。只是为了说明 this 实例与传入的 other 对象进行比较。

编辑

您可以使用对 IEqualityComparer<Resumenes> 的覆盖,但是您必须将它传递给哈希集的构造函数。但是你放入HashSet中的数据对象实现IEqualityComparer是相当少见的。你的 Resumenes 最好实现 IEquatable<T> 接口

public class Resumenes: INotifyPropertyChanged, IEquatable<Resumenes> {


    public override bool Equals(object obj) { ... }
    public bool Equals(Resumenes other) { ... }
    public override int GetHashCode() { ... }
}

这里有几件事:

  1. 因为 Nombre 实际上是散列键,如果它在项目在散列中时发生变化:所有赌注都关闭;避免这种情况的一个简单方法是将其设置为只读
  2. 有叶型的器具很奇怪IEqualityComparer<T>;我想知道这是否是问题的很大一部分 - 特别是如果您没有将显式比较器传递到哈希集中;但是,老实说,在此处实施 IEquatable<T> 会更简单和更可取:
public class Resumenes : INotifyPropertyChanged, IEquatable<Resumenes>
{
        // ...
        public override bool Equals(object obj) => obj is Resumenes other && Equals(other);
        public bool Equals(Resumenes other)
            => other is not null && other.Nombre == this.Nombre && other.Descripcion == this.Descripcion;

        public override int GetHashCode()
            => Nombre.GetHashCode();
}

可以使用自定义相等比较器执行此操作,但您需要将此类比较器显式传递到 new HashSet<Resumenes>(comparer) 构造函数中。我希望这个比较器是 不同类型 的单例实例,例如 ResumenesComparer.Instance。使用 IEquatable<T> 更加明显和方便。