Versions Compared

Key

  • This line was added.
  • This line was removed.
  • Formatting was changed.
Comment: Migration of unmigrated content due to installation of a new plugin

...

Code Block
[Serializable]
[IntroduceInterface( typeof(INotifyPropertyChanged),
                     OverrideAction = InterfaceOverrideAction.Ignore )]
[MulticastAttributeUsage( MulticastTargets.Class,
                          Inheritance = MulticastInheritance.Strict )]
public sealed class NotifyPropertyChangedAttribute :
   InstanceLevelAspect, INotifyPropertyChanged
{

    [ImportMember( "OnPropertyChanged", IsRequired = false )]
    public Action<string> BaseOnPropertyChanged;

    [IntroduceMember( Visibility = Visibility.Family,
                      IsVirtual = true,
                      OverrideAction = MemberOverrideAction.Ignore )]
    public void OnPropertyChanged( string propertyName )
    {
        if ( this.PropertyChanged != null )
        {
            this.PropertyChanged( this.Instance,
               new PropertyChangedEventArgs( propertyName ) );
        }
    }

    [IntroduceMember( OverrideAction = MemberOverrideAction.Ignore )]
    public event PropertyChangedEventHandler PropertyChanged;

    [OnLocationSetHandler,
     MulticastSelector( Targets = MulticastTargets.Property )]
    public void OnPropertySet( LocationInterceptionArgs args )
    {
        if ( args.Value == args.GetCurrentValue() ) return;

        if ( this.BaseOnPropertyChanged != null )
        {
            this.BaseOnPropertyChanged( args.Location.PropertyInfo.Name );
        }
        else
        {
            this.OnPropertyChanged( args.Location.PropertyInfo.Name );
        }
    }
}

In stead of using a complex compound aspect we can now introduce members and specify an action if the member is already there. The PropertyChanged event member will be added to the class if it is not already on the class. Nice code (smile) Unfortunately PostSharp 2 is far from ready and even the samples don't work completely (sad)

Another downside is that is not backwards compatible with 1.5 so existing aspects need to reviewed.

I added a test solution to https://repos.deltares.nl/repos/delft-tools/trunk/sandbox/PostSharp2
to play around with the new post sharp.

Gael introducing the attribute : http://www.postsharp.org/blog/introducing-postsharp-20-1-notifypropertychanged