You are viewing an old version of this page. View the current version.

Compare with Current View Page History

« Previous Version 3 Next »

With the introduction of PostSharp 2 a lot of code will be more readable.
Look at the implementation for property changed:

[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

  • No labels