Property declaration snippets for ReSharper
Automatic properties in .NET 3.5 are a nice thing, but they are out of the equation if you want to take advantage of the almighty INotifyPropertyChanged interface which plays a crucial role in WPF. Here’s a set of ReSharper snippets that simplify interface implementation and property declaration.
Event Declaration
The first snippet just prints out an interface implementation and adds some additional value to it, including a runtime check of property names in Debug builds (credits go to Josh Smith for this one). Just type inpc to print out this statement:
#region INotifyPropertyChanged event ///<summary> ///Occurs when a property value changes. ///</summary> public event PropertyChangedEventHandler PropertyChanged; /// <summary> /// Raises the <see cref="PropertyChanged"/> event for /// a given property. /// </summary> /// <param name="propertyName">The name of the changed property.</param> protected void OnPropertyChanged(string propertyName) { //validate the property name in debug builds VerifyProperty(propertyName); if (PropertyChanged != null) { PropertyChanged(this, new PropertyChangedEventArgs(propertyName)); } } /// <summary> /// Verifies whether the current class provides a property with a given /// name. This method is only invoked in debug builds, and results in /// a runtime exception if the <see cref="OnPropertyChanged"/> method /// is being invoked with an invalid property name. This may happen if /// a property's name was changed but not the parameter of the property's /// invocation of <see cref="OnPropertyChanged"/>. /// </summary> /// <param name="propertyName">The name of the changed property.</param> [Conditional("DEBUG")] private void VerifyProperty(string propertyName) { Type type = this.GetType(); //look for a *public* property with the specified name PropertyInfo pi = type.GetProperty(propertyName); if (pi == null) { //there is no matching property - notify the developer string msg = "OnPropertyChanged was invoked with invalid property name {0}: "; msg += "{0} is not a public property of {1}."; msg = String.Format(msg, propertyName, type.FullName); Debug.Fail(msg); } } #endregion
…and don’t forget to declare the INotifyPropertyChanged implementation. The snippet won’t do that for you:
public class Class1 : INotifyPropertyChanged
Property Declaration
The second snippet (shortcut is pcp) simplifies the declaration of a given property for you. You can define property name, type, default value and a description summary. The generated code for a sample property looks like this:
#region UserId /// <summary> /// The numeric ID of the user. Defaults to null. /// </summary> private int? userId = null; /// <summary> /// The numeric ID of the user. Defaults to null. /// </summary> public int? UserId { get { return userId; } set { //ignore if values are equal if (value == userId) return; userId = value; OnPropertyChanged("UserId"); } } #endregion
Grab the snippet here: Download
I like it!
Checking the propertyName in the PropertyChanged event is one of the reasons I created the automatic class tester that tests properties and that they implement INotifyPropertyChanged properly. Hopefully a useful alternative if you use unit testing at all:
http://www.thejoyofcode.com/Automatically_Unit_Test_your_Model.aspx
Josh Twist
Josh,
Thanks for the link – the framework sure makes a nice addition to the toolbox 🙂
How do I add this to Resharper?