Skip to content

Instantly share code, notes, and snippets.

@mgroves
Created February 22, 2012 15:30
Show Gist options
  • Select an option

  • Save mgroves/1885556 to your computer and use it in GitHub Desktop.

Select an option

Save mgroves/1885556 to your computer and use it in GitHub Desktop.
notifypropertyweaver example
// this is how your code might look without using notifypropertyweaver
public class Person1 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
private void NotifyPropertyChanged(string propertyName)
{
if (PropertyChanged != null)
{
PropertyChanged(this, new PropertyChangedEventArgs(propertyName));
}
}
string _givenName;
public string GivenName
{
get { return _givenName; }
set
{
if(value != _givenName)
{
_givenName = value;
NotifyPropertyChanged("GivenName");
NotifyPropertyChanged("FullName");
}
}
}
string _familyName;
public string FamilyName
{
get { return _familyName; }
set
{
if (value != _familyName)
{
_familyName = value;
NotifyPropertyChanged("FamilyName");
NotifyPropertyChanged("FullName");
}
}
}
public string FullName
{
get
{
return string.Format("{0} {1}", GivenName, FamilyName);
}
}
}
// this is how your code would look WITH using notifyproperty weaver
public class Person2 : INotifyPropertyChanged
{
public event PropertyChangedEventHandler PropertyChanged;
public string GivenName { get; set; }
public string FamilyName { get; set; }
public string FullName
{
get
{
return string.Format("{0} {1}", GivenName, FamilyName);
}
}
}
// this is how the Person2 class looks when you decompile it
public class Person2 : INotifyPropertyChanged
{
private PropertyChangedEventHandler PropertyChanged;
public string FamilyName
{
get;
set
{
if (string.Equals(this.<FamilyName>k__BackingField, value))
{
return;
}
this.<FamilyName>k__BackingField = value;
this.OnPropertyChanged("FullName");
this.OnPropertyChanged("FamilyName");
}
}
public string FullName
{
get
{
return string.Format("{0} {1}", this.GivenName, this.FamilyName);
}
}
public string GivenName
{
get;
set
{
if (string.Equals(this.<GivenName>k__BackingField, value))
{
return;
}
this.<GivenName>k__BackingField = value;
this.OnPropertyChanged("FullName");
this.OnPropertyChanged("GivenName");
}
}
public Person2()
{
}
public virtual void OnPropertyChanged(string propertyName)
{
PropertyChangedEventHandler propertyChanged = this.PropertyChanged;
if (propertyChanged != null)
{
base(new PropertyChangedEventArgs(propertyName));
}
}
public event PropertyChangedEventHandler PropertyChanged;
}
<!-- this is all that's added to your project -->
<UsingTask TaskName="NotifyPropertyWeaverMsBuildTask.WeavingTask" AssemblyFile="$(SolutionDir)Tools\NotifyPropertyWeaverMsBuildTask.dll" />
<Target Name="AfterCompile">
<NotifyPropertyWeaverMsBuildTask.WeavingTask />
</Target>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment