postSharp,INotifyPropertyChanged的AOP植入

postSharp,INotifyPropertyChanged的AOP植入

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Linq;
using System.Text;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;

namespace U1City.Infrastructure.MVP
{
    [Serializable]
    [IntroduceInterface(typeof(INotifyPropertyChanged), OverrideAction = InterfaceOverrideAction.Ignore)]
    public class NotifyPropertyChangedAttribute : InstanceLevelAspect, INotifyPropertyChanged
    {

        [OnLocationSetValueAdvice, MulticastPointcut(Targets = MulticastTargets.Property)]
        public void OnValueChanged(LocationInterceptionArgs args)
        {
            var current = args.GetCurrentValue();
            if ((args.Value != null && (!args.Value.Equals(current)))
                || (current != null && (!current.Equals(args.Value))))
            {
                args.ProceedSetValue();
                this.OnRaisePropertyChange(args.Location.Name);
            }
        }

        #region INotifyPropertyChanged 成员

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




        protected void OnRaisePropertyChange(string property)
        {
            if (PropertyChanged != null)
            {
                PropertyChanged.Invoke(this.Instance, new PropertyChangedEventArgs(property));
            }
        }
        #endregion
    }
}

 

 

posted @ 2013-12-04 18:33  bert.zeng  阅读(232)  评论(0)    收藏  举报