|
Project Information
Members
Featured
Downloads
Links
|
PropFuPropFu is a plugin for PostSharp that eliminates the mind numbing and somewhat risky repetition of declaring properties that support INotifyPropertyChanged. If you work with WPF, or WinForms you know how painful this can be. With this simple plugin to the PostSharp aspect library all of your properties can magically be turned into automatic properties without a backing field, and all of your properties will call a method of your choosing to fire the PropertyChanged event. All you need to do is add one attribute to the top of your class, and one attribute to the method that will fire the event (usually in a thin, common base class) and you're done! Everything is done at compile time and you will receive absolutely no runtime performance hit. Once you start using this technique you'll never go back. Note: This can be used in commercial/closed source software as the only code you will be linking to directly in your project is under the MIT license. The plug-in is GPL, but that does not transfer over to an app modified by the plugin: http://www.postsharp.org/forum/post1416.html#p1416 Here's what you had to do before: public class PersonLame : INotifyPropertyChanged
{
private string _name;
public string Name
{
get { return _name; }
set
{
if (value == _name)
return;
_name = value;
OnPropertyChanged("Name");
}
}
private int _age;
public int Age
{
get { return _age; }
set
{
if (value == _age)
return;
_age = value;
OnPropertyChanged("Age");
}
}
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}Here's the same as above using PropFu: [NotifyPropertyChanged]
public class PersonAwesome : INotifyPropertyChanged
{
public string Name { get; set; }
public int Age { get; set; }
#region INotifyPropertyChanged Members
public event PropertyChangedEventHandler PropertyChanged;
#endregion
[OnPropertyChanged]
private void OnPropertyChanged(string propertyName)
{
if (PropertyChanged == null)
return;
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}How it works: You simply mark classes with the NotifyPropertyChanged attribute, when you want to have properties that fire the property changed event. You mark a method, anywhere in your class hierarchy with the OnPropertyChangedAttribute, and that method will be called when the property value is changed. The system even injects a guard clause that checks to see if the value has changed. Notes:
InstallationWhen PostSharp is installedCopy files PropFu.psplugin, PropFu.Weaver.dll, and PropFu.dll into C:\Program Files\PostSharp 1.0\PlugIns (for global installation) or C:\Documents and Settings\userName\Application Data\PostSharp 1.0 (user-only installation). You need to link to the PropFu.dll in your code to get access to the attributes. When you are using only the binaries of PostSharpIf you are going to have PropFu, and PostSharp checked into your source control system, and do not wish to make all your developers/build machines install PostSharp, you need to do a bit of manual work per project. To inject aspects into an assembly, PostSharp waits for the project to compile, and then during post-build reads in the assemblies and starts weaving code at the IL level. To make this happen, you need to do the following:
<PropertyGroup> <DontImportPostSharp>True</DontImportPostSharp> </PropertyGroup> <Import Project="$(MSBuildBinPath)\Microsoft.CSharp.targets" /> <Import Project="..\..\..\PostSharp\PostSharp.targets"/> You probably need to change the relative path in the above snippet, depending on where your project is located relative to PostSharp directory. |