PostSharp AOP
2011-03-14 17:50
302 查看
选用 PostSharp 的好处有:
1.采用 MSIL 静态代码注入,避免使用反射;
2.使用 MSBuild Task,使得开发人员可以像使用编译器内置 Attribute 那样使用 AOP;
3.可以拦截任意方法而不是局限于 virtual 方法;
4.对调用方法有更多的控制方法,比如输入参数,返回结果等
1 class Start
2 {
3 static void Main() {
4 Speak("我开始哈");
5 EndSpeak();
6 Console.ReadKey();
7 }
8
9 [Posts("开始说话了..")]
10 private static void Speak(string message) {
11 Console.WriteLine(".{0}.....正式执行了....", message);
12 }
13
14 [Posts("error:")]
15 [onException("endspeak")]
16 static void EndSpeak() {
17 Console.WriteLine("会发生错误的调用......");
18 throw new Exception("====== 这是故意发生的错误.===");
19 }
20 }
21
22
23 ///////////////////////////////////////
24 aop
25 [Serializable]
26 [global::System.AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
27 public class PostsAttribute : OnMethodBoundaryAspect
28 {
29
30 private string _msg = string.Empty;
31 public PostsAttribute(string message)
32 {
33 _msg = message;
34 }
35
36 public override void OnEntry(MethodExecutionEventArgs args)
37 {
38 if (new PermissionHandler().Add())//判断权限
39 {
40 Console.WriteLine(_msg + "mmmmmmmmmmm...{0}...onEntry......", DateTime.Now);
41 // base.OnEntry(args);
42 }
43 else
44 {
45 args.ReturnValue = false;
46 args.FlowBehavior = FlowBehavior.Return;
47 }
48 }
49
50 public override void OnSuccess(MethodExecutionEventArgs eventArgs)
51 {
52 base.OnSuccess(eventArgs);
53 }
54
55 public override void OnExit(MethodExecutionEventArgs args)
56 {
57 Console.WriteLine("...{0}...OnExit......", DateTime.Now);
58 base.OnExit(args);
59 }
60 }
61
62 //权限判断
63 public class PermissionHandler
64 {
65 /// <summary>
66 /// 判断是否有权限
67 /// </summary>
68 /// <returns></returns>
69 public bool Add()
70 {
71 return false;
72 }
73 }
1.采用 MSIL 静态代码注入,避免使用反射;
2.使用 MSBuild Task,使得开发人员可以像使用编译器内置 Attribute 那样使用 AOP;
3.可以拦截任意方法而不是局限于 virtual 方法;
4.对调用方法有更多的控制方法,比如输入参数,返回结果等
1 class Start
2 {
3 static void Main() {
4 Speak("我开始哈");
5 EndSpeak();
6 Console.ReadKey();
7 }
8
9 [Posts("开始说话了..")]
10 private static void Speak(string message) {
11 Console.WriteLine(".{0}.....正式执行了....", message);
12 }
13
14 [Posts("error:")]
15 [onException("endspeak")]
16 static void EndSpeak() {
17 Console.WriteLine("会发生错误的调用......");
18 throw new Exception("====== 这是故意发生的错误.===");
19 }
20 }
21
22
23 ///////////////////////////////////////
24 aop
25 [Serializable]
26 [global::System.AttributeUsage(AttributeTargets.All, AllowMultiple = true)]
27 public class PostsAttribute : OnMethodBoundaryAspect
28 {
29
30 private string _msg = string.Empty;
31 public PostsAttribute(string message)
32 {
33 _msg = message;
34 }
35
36 public override void OnEntry(MethodExecutionEventArgs args)
37 {
38 if (new PermissionHandler().Add())//判断权限
39 {
40 Console.WriteLine(_msg + "mmmmmmmmmmm...{0}...onEntry......", DateTime.Now);
41 // base.OnEntry(args);
42 }
43 else
44 {
45 args.ReturnValue = false;
46 args.FlowBehavior = FlowBehavior.Return;
47 }
48 }
49
50 public override void OnSuccess(MethodExecutionEventArgs eventArgs)
51 {
52 base.OnSuccess(eventArgs);
53 }
54
55 public override void OnExit(MethodExecutionEventArgs args)
56 {
57 Console.WriteLine("...{0}...OnExit......", DateTime.Now);
58 base.OnExit(args);
59 }
60 }
61
62 //权限判断
63 public class PermissionHandler
64 {
65 /// <summary>
66 /// 判断是否有权限
67 /// </summary>
68 /// <returns></returns>
69 public bool Add()
70 {
71 return false;
72 }
73 }
相关文章推荐
- 使用PostSharp在.NET平台上实现AOP
- 使用PostSharp在.NET平台上实现AOP
- PostSharp的AOP设计在.NET Remoting中的应用
- AOP - PostSharp 2.0
- PostSharp Aop(安装)
- AOP之PostSharp3-MethodInterceptionAspect
- AOP 系列: 使用PostSharp在.NET平台上实现AOP
- AOP:PostSharp和Castle DynamicProxy
- 使用PostSharp在.NET平台上实现AOP
- AOP之PostSharp
- AOP之PostSharp3-MethodInterceptionAspect
- .NET下的AOP: PostSharp 原理分析
- PostSharp:不错的aop框架
- .NET下的AOP: PostSharp 原理分析
- PostSharp AOP学习研究(一)
- Aop面向方面编程之PostSharp框架
- PostSharp AOP编程:1.基于PostSharp的AOP日志记录和异常捕捉【附带源码】
- 基于PostSharp的AOP框架设计
- PostSharp AOP 学习研究(二)
- PostSharp AOP编程:1.基于PostSharp的AOP日志记录和异常捕捉【附带源码】