有没有办法用ReSharper自动生成GetHashCode和Equals ?

时间:2021-12-08 13:54:33

In eclipse, when I code in Java, there is a feature to auto-generate a basic, efficient, and bug free implementation of hashCode() and equals() without consuming brain power.

在eclipse中,当我用Java编写代码时,有一个特性可以自动生成基本的、高效的、无bug的hashCode()和equals()实现,而不需要耗费脑力。

Is there a similar feature either built-in in Visual Studio or in ReSharper ?

在Visual Studio或ReSharper中是否有类似的特性?

2 个解决方案

#1


52  

Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu (Alt+Ins depending on settings or Resharper -> Edit -> Generate Code), and select “Equality members”:

是的,Resharper做到了。将光标放在你的类型中,打开“生成代码”菜单(Alt+Ins取决于设置或Resharper ->编辑->生成代码),选择“平等成员”:

有没有办法用ReSharper自动生成GetHashCode和Equals ?

This opens a window where you can select which members are used for equality, along with some options about the generated code (e.g. should your type implement IEquatable<T>):

这将打开一个窗口,在那里您可以选择使用哪些成员进行平等,以及关于生成代码的一些选项(例如,您的类型是否实现了IEquatable ):

有没有办法用ReSharper自动生成GetHashCode和Equals ?

If you start with a simple type with two properties:

如果您从具有两个属性的简单类型开始:

class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
}

Then the generated code may look something like:

然后生成的代码看起来可能如下所示:

class Person : IEquatable<Person>
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public bool Equals(Person other)
    {
        if (ReferenceEquals(null, other))
            return false;
        if (ReferenceEquals(this, other))
            return true;
        return string.Equals(FirstName, other.FirstName) && string.Equals(LastName, other.LastName);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;
        if (ReferenceEquals(this, obj))
            return true;
        if (obj.GetType() != this.GetType())
            return false;
        return Equals((Person)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((FirstName != null ? FirstName.GetHashCode() : 0) * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
        }
    }
}

#2


0  

Since you asked if also Visual Studio can do that: since XI.2017 it finaly can generate something useful.

既然你问过Visual Studio是否也能做到这一点:从2017年起,它最终会产生一些有用的东西。

Using ctr+. inside class and choosing "Generate Equals and GetHashCode"

使用ctr +。在类中选择“生成Equals和GetHashCode”

See https://*.com/a/48441971/4547594

参见https://*.com/a/48441971/4547594

#1


52  

Yes, Resharper can do that. With cursor inside your type, open the “Generate code” menu (Alt+Ins depending on settings or Resharper -> Edit -> Generate Code), and select “Equality members”:

是的,Resharper做到了。将光标放在你的类型中,打开“生成代码”菜单(Alt+Ins取决于设置或Resharper ->编辑->生成代码),选择“平等成员”:

有没有办法用ReSharper自动生成GetHashCode和Equals ?

This opens a window where you can select which members are used for equality, along with some options about the generated code (e.g. should your type implement IEquatable<T>):

这将打开一个窗口,在那里您可以选择使用哪些成员进行平等,以及关于生成代码的一些选项(例如,您的类型是否实现了IEquatable ):

有没有办法用ReSharper自动生成GetHashCode和Equals ?

If you start with a simple type with two properties:

如果您从具有两个属性的简单类型开始:

class Person
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }
}

Then the generated code may look something like:

然后生成的代码看起来可能如下所示:

class Person : IEquatable<Person>
{
    public string FirstName { get; private set; }
    public string LastName { get; private set; }

    public bool Equals(Person other)
    {
        if (ReferenceEquals(null, other))
            return false;
        if (ReferenceEquals(this, other))
            return true;
        return string.Equals(FirstName, other.FirstName) && string.Equals(LastName, other.LastName);
    }

    public override bool Equals(object obj)
    {
        if (ReferenceEquals(null, obj))
            return false;
        if (ReferenceEquals(this, obj))
            return true;
        if (obj.GetType() != this.GetType())
            return false;
        return Equals((Person)obj);
    }

    public override int GetHashCode()
    {
        unchecked
        {
            return ((FirstName != null ? FirstName.GetHashCode() : 0) * 397) ^ (LastName != null ? LastName.GetHashCode() : 0);
        }
    }
}

#2


0  

Since you asked if also Visual Studio can do that: since XI.2017 it finaly can generate something useful.

既然你问过Visual Studio是否也能做到这一点:从2017年起,它最终会产生一些有用的东西。

Using ctr+. inside class and choosing "Generate Equals and GetHashCode"

使用ctr +。在类中选择“生成Equals和GetHashCode”

See https://*.com/a/48441971/4547594

参见https://*.com/a/48441971/4547594