PostSharp AOP
选用 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 }
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 }