AOP之PostSharp4-实现类INotifyPropertyChanged植入
2011-12-10 20:58 破狼 阅读(2704) 评论(2) 编辑 收藏 举报在前面几篇PostSharp的随笔,今天来一个简单的demo。PostSharp的其他内容将会在后面继续更新。
如果我们了解wpf或者silverlight开发中的MVVM模式,就知道框架要求我们的ViewModel必须实现INotifyPropertyChanged,来得到属性改变的事件通知,更新UI。
实现INotifyPropertyChanged接口很简单,而且一沉不变,属于重复劳动。在这里我们将看看如何运用PostSharp来解决我们的重复劳动。当然这里只是一个demo演示,具体在项目开发中你直接实现INotifyPropertyChanged,或者AOP植入,这取决我个人和团队文化。
在下面我们将会用到Postsharp的:
- IntroduceMember:向目标对象植入成员。
- IntroduceInterface:使得目标实现接口,参数接口类型。
- 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
}
}
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));
}
{
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
{ get; set; }
public string Sex
{ get; set; }
public int ID
{ get; set; }
}
public class Student
{
public string Name
{ get; set; }
public string Sex
{ get; set; }
public int ID
{ get; set; }
}
运行效果如下:
附件下载:demo
本博客中相关文章有:
作者:破 狼
出处:http://www.cnblogs.com/whitewolf/
本文版权归作者,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利。该文章也同时发布在我的独立博客中-个人独立博客、博客园--破狼和51CTO--破狼。