代码改变世界

AOP之PostSharp4-实现类INotifyPropertyChanged植入

  破狼  阅读(2708)  评论(2编辑  收藏  举报

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

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

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

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

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

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

复制代码
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
    }
}
复制代码

测试代码:

复制代码
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)); 
        }
复制代码
复制代码
[INotifyPropertyChanged] 
   public class Student 
   { 
       public string Name 
       { getset; } 

       public string Sex 
       { getset; } 

       public int ID 
       { getset; } 
   }
复制代码
运行效果如下:

image

附件下载: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
  • 编辑推荐:
    · 如何编写易于单元测试的代码
    · 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
    · .NET Core 中如何实现缓存的预热?
    · 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
    · AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
    阅读排行:
    · 10年+ .NET Coder 心语 ── 封装的思维:从隐藏、稳定开始理解其本质意义
    · 地球OL攻略 —— 某应届生求职总结
    · 提示词工程——AI应用必不可少的技术
    · Open-Sora 2.0 重磅开源!
    · 周边上新:园子的第一款马克杯温暖上架
    点击右上角即可分享
    微信分享提示