在前面几篇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
}
}
测试代码:
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
{ get; set; }
public string Sex
{ get; set; }
public int ID
{ get; set; }
}
运行效果如下:
附件下载: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/archive/2011/12/10/PostSharp4.html,如需转载请自行联系原作者