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

1 Comment

  1. Unknown User (don)

    Let's wait for a release.

    Actually (I reformated code a bit) it does not look that bad. Especially if you tried to implement aspects once where you need to skip interface if it is already implemented. Those attributes seem to be cleaner compare to the current implementation that we use.