Code Generation Improvements in ReSharper 7

Posted on by

If there was a competition between different ReSharper subsystems on which one produces most value relative to developer effort, the Generate action would win that competition outright. And, needless to say, in the 7.0 release we have added a couple of new features and improvements to ReSharper’s intuitive code generator.

Generate Partial Methods

Many code-generating frameworks produce code made using partial classes, with the assumption that developers can augment generated functionality by implementing partial methods when needed. For example, the Entity Framework does this (in non-Code-First scenarios, of course) by defining entity classes as partial .

ReSharper now supports this scenario. To use it, create a partial declaration of a class, then choose Generate|Partial Methods:

Now, simply pick the partial methods that you want to implement:

All the partial methods are consequently stubbed out in the target class, ready for you to implement.

Improvements in Generate Equality Members

When comparing types, many of our users wanted to control the way type checks were performed. So, by popular demand, the Generate Equality Members dialog now has three different options for implementing the type comparison:

Depending on which of the three options you choose in the above, the condition for checking the type in the Equals() method will appear as one of the following:

Type exactly same as this :



Type exactly as owner type:



Type is of owner type or subtype:



Generate Equality Comparer

In addition to the inline equality implementation for a particular class, ReSharper 7 comes with a brand-new action called Generate Equality Comparer. The goal of this action is to generate a separate class that implements the IEqualityComparer<T> interface. This is useful for creating specific comparison criteria, as well as for instances where an IEqualityComparer is explicitly required by a particular API.

For example, given a simple Person class with two properties, we can fire up the action:

and, after picking the members for comparison, Person gets the following private inner class:

In addition, the option to generate the static instance of the comparer adds the following field to the Person class:

Support for INotifyPropertyChanged

And one last thing… if you’ve got a class that implements the INotifyPropertyChanged or inherits from a class that supports property change notifications (e.g., Prism’s NotificationObject ), the Generate|Properties action offers an option to implement properties with change notification calls:

By the way, support for INotifyPropertyChanged in ReSharper 7 is more than just generation, so make sure to read more about this.