且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

AOP之PostSharp4-实现类INotifyPropertyChanged植入

更新时间:2022-08-29 08:44:00

        在前面几篇PostSharp的随笔,今天来一个简单的demo。PostSharp的其他内容将会在后面继续更新。

      如果我们了解wpf或者silverlight开发中的MVVM模式,就知道框架要求我们的ViewModel必须实现INotifyPropertyChanged,来得到属性改变的事件通知,更新UI。

实现INotifyPropertyChanged接口很简单,而且一沉不变,属于重复劳动。在这里我们将看看如何运用PostSharp来解决我们的重复劳动。当然这里只是一个demo演示,具体在项目开发中你直接实现INotifyPropertyChanged,或者AOP植入,这取决我个人和团队文化。

     在下面我们将会用到Postsharp的:

  1. IntroduceMember:向目标对象植入成员。
  2. IntroduceInterface:使得目标实现接口,参数接口类型。
  3. OnLocationSetValueAdvice:PostSharp的一种Advice Aspect。

   下面我就看看我们的代码是如何简洁来实现:

AOP之PostSharp4-实现类INotifyPropertyChanged植入
using System;
using System.ComponentModel;
using PostSharp.Aspects;
using PostSharp.Aspects.Advices;
using PostSharp.Extensibility;

namespace PostSharpDemo
{
    [Serializable]
    [IntroduceInterface(typeof(INotifyPropertyChanged), OverrideAction = InterfaceOverrideAction.Ignore)]
    public class INotifyPropertyChangedAttribute : 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
    }
}
AOP之PostSharp4-实现类INotifyPropertyChanged植入

测试代码:

AOP之PostSharp4-实现类INotifyPropertyChanged植入
static void Main(string[] args) 
        { 

             Student stu = new Student(); 
            (stu as INotifyPropertyChanged).PropertyChanged += new PropertyChangedEventHandler(Program_PropertyChanged); 
            stu.ID = 10
            stu.Name = "wolf"
            stu.Sex = "Man"
            stu.ID = 2
            Console.Read(); 
        } 

        static void Program_PropertyChanged(object sender, PropertyChangedEventArgs e) 
        { 
            Console.WriteLine(string.Format("property {0} has changed", e.PropertyName)); 
        }
AOP之PostSharp4-实现类INotifyPropertyChanged植入
AOP之PostSharp4-实现类INotifyPropertyChanged植入
[INotifyPropertyChanged] 
   public class Student 
   { 
       public string Name 
       { getset; } 

       public string Sex 
       { getset; } 

       public int ID 
       { getset; } 
   }
AOP之PostSharp4-实现类INotifyPropertyChanged植入
运行效果如下:

AOP之PostSharp4-实现类INotifyPropertyChanged植入

附件下载:demo

本博客中相关文章有:

AOP之PostSharp初见-OnExceptionAspect AOP之PostSharp2-OnMethodBoundaryAspect AOP之PostSharp3-MethodInterceptionAspect AOP之PostSharp4-实现类INotifyPropertyChanged植入 AOP之PostSharp5-LocationInterceptionAspect AOP之PostSharp6-EventInterceptionAspect http://www.cnblogs.com/whitewolf/category/312638.html


作者:破  狼 
出处:http://www.cnblogs.com/whitewolf/ 
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客博客园--破狼51CTO--破狼。http://www.cnblogs.com/whitewolf/archive/2011/12/10/PostSharp4.html