Can ReSharper generate an IEqualityComparer implementation

I am aware, that ReSharper can generate equality members for a class through 'ReSHarper > Edit > Generate Code'. But is it also possible to generate an implementation for an 'IEqualityComparer'?

For the sake of example, lets assume that an API I am not in control of has the following class.

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

public string LastName { get; set; }
}

Now as the user of this API I want to be able to compare instances of this class or store those in an Dictionary or HashSet. Since the implementation of Person does not provide equality members I have to implement an IEqualityComparer<Person>. This leads me to the following implementation:

public class PersonComparer : IEqualityComparer<Person>
{
public bool Equals(Person x, Person y)
{
throw new NotImplementedException();
}

public int GetHashCode(Person obj)
{
throw new NotImplementedException();
}
}

Now the question is, can I make ReSharper generate the code for the above Equals() and GetHashCode() for me? Just like I could generate those methods through 'ReSHarper > Edit > Generate Code' for a single class?

0
2 comments
Official comment

Hello Mike,

 

thank you for the feedback.

There's Generate Equality Comparer feature that provides the specified behavior, please refer to the following help article - https://www.jetbrains.com/help/resharper/Code_Generation__Generating_Equality_Comparer.html.

Thank you.

Hello Angelina,

thank you for your answer!

But the Generate Equality Comparer Feature only works, if the type I'm generating the comparer for is defined in the same library. Let me try to give another example. 

Let's assume I have an application, that consists of the following code:

class Program
{
public string Text { get; }

static void Main(string[] args)
{
//For sake of example I used 'FileInfo'. But this could by any type defined in a third-party library.
var file = new System.IO.FileInfo("C:\\some\\path\\to\\file.txt");
}

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

public string LastName { get; set; }
}
}

Now, if I place the caret somewhere inside the definition of the type Person and hit Alt + Insert to generate an Equality Comparer ReSharper opens a dialog that lets me choose the properties that should take part in the comparison (in the above case FirstName and LastName). Now, I want to generate an Equality Comparer for System.IO.FileInfo (the .NET-Framework type).

To do so, I place the caret on System.IO.FileInfo and hit Alt + Insert to generate an Equality Comparer. This again opens the ReSharper dialog that lets me choose the properties that should take part in the comparison. However, it does not list the public properties of System.IO.FileInfo and instead lists the properties of the type the line of code is defined in (which is in the above case the property Text defined in the class Program).

An now I'm just wondering if there is any way that lets me generate an Equality Comparer for a .NET-Framework type or any other third-party type?

1

Please sign in to leave a comment.