Archive

Posts Tagged ‘ReSharper’

Property declaration snippets for ReSharper

March 21st, 2008

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

Author: Categories: C#, ReSharper, WPF Tags: , ,

ReSharper Code Snippet for Dependency Properties

January 17th, 2008

This is a ReSharper code snippet I’ve been using quite a lot lately – it allows you to easily create a WPF dependency property along with event handlers, documentation, and initialization code. Here’s a sample:

#region MyStringProperty dependency property

/// <summary>
/// This is a sample string property.
/// </summary>
public static readonly DependencyProperty MyStringPropertyProperty;

//TODO: copy to static constructor
//register dependency property
//FrameworkPropertyMetadata md = new FrameworkPropertyMetadata("hello world", MyStringPropertyPropertyChanged);
//MyStringPropertyProperty = DependencyProperty.Register("MyStringProperty", typeof (string), typeof (MyControl), md);                                                      


/// <summary>
/// A property wrapper for the <see cref="MyStringPropertyProperty"/>
/// dependency property:<br/>
/// This is a sample string property.
/// </summary>
public string MyStringProperty
{
  get { return (string) GetValue(MyStringPropertyProperty); }
  set { SetValue(MyStringPropertyProperty, value); }
}


/// <summary>
/// Handles changes on the <see cref="MyStringPropertyProperty"/> dependency property. As
/// WPF internally uses the dependency property system and bypasses the
/// <see cref="MyStringProperty"/> property wrapper, updates should be handled here.
/// </summary>
/// <param name="d">The currently processed owner of the property.</param>
/// <param name="e">Provides information about the updated property.</param>
private static void MyStringPropertyPropertyChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
{
  MyControl owner = (MyControl) d;
  string newValue = (string) e.NewValue;

  //TODO provide implementation
  throw new NotImplementedException("Change event handler for dependency property MyStringProperty not implemented.");
}

#endregion

Just import the attached snippet into ReSharper, then type dpp in Visual Studio to trigger the template.

BTW: If you’re using VS without ReSharper but need snippets for dependency properties, routed events or routed commands, Dr. WPF has the cure.

Download File: dpp-snippet.xml