zoukankan      html  css  js  c++  java
  • AOP之PostSharp4实现类INotifyPropertyChanged植入

            在前面几篇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
查看全文
  • 相关阅读:
    PageAdmin Cms系统如何修改点击数(浏览数)
    最简js深浅拷贝说明
    实用的前端网址
    js的一些编码问题
    自己写一个文字过长显示省略号的函数
    垂直居中 和 水平居中
    js原码工具集
    url 、src 、href 的区别
    transform对定位元素的影响
    css实现一色多变化
  • 原文地址:https://www.cnblogs.com/whitewolf/p/PostSharp4.html
  • Copyright © 2011-2022 走看看