0
|
1 using System;
|
|
2 using System.Reflection;
|
|
3
|
|
4 using BLToolkit.Aspects;
|
|
5 using BLToolkit.Reflection;
|
|
6 using BLToolkit.TypeBuilder;
|
|
7 using BLToolkit.TypeBuilder.Builders;
|
|
8
|
|
9 using NUnit.Framework;
|
|
10
|
|
11 namespace Aspects
|
|
12 {
|
|
13 [TestFixture]
|
|
14 public class InterceptorAspectTest
|
|
15 {
|
|
16 public class TestInterceptor : IInterceptor
|
|
17 {
|
|
18 public void Init(CallMethodInfo info, string configString)
|
|
19 {
|
|
20 }
|
|
21
|
|
22 public void Intercept(InterceptCallInfo info)
|
|
23 {
|
|
24 if (info.CallMethodInfo.MethodInfo.ReturnType == typeof(int))
|
|
25 info.ReturnValue = 10;
|
|
26 }
|
|
27 }
|
|
28
|
|
29 [Interceptor(typeof(TestInterceptor), InterceptType.BeforeCall | InterceptType.OnCatch)]
|
|
30 public abstract class TestClass
|
|
31 {
|
|
32 public abstract int Test(int i1, ref int i2, out int i3, out decimal d4);
|
|
33
|
|
34 [NoInterception(typeof(TestInterceptor), InterceptType.BeforeCall | InterceptType.OnCatch)]
|
|
35 public abstract int TestNo();
|
|
36
|
|
37 public class PropInterceptor : Interceptor
|
|
38 {
|
|
39 protected override void BeforeCall(InterceptCallInfo info)
|
|
40 {
|
|
41 info.Items["ReturnValue"] = info.ReturnValue;
|
|
42 }
|
|
43
|
|
44 protected override void AfterCall(InterceptCallInfo info)
|
|
45 {
|
|
46 info.ReturnValue = (int)info.ReturnValue + (int)info.Items["ReturnValue"];
|
|
47 }
|
|
48 }
|
|
49
|
|
50 [Interceptor(
|
|
51 typeof(PropInterceptor), InterceptType.BeforeCall | InterceptType.AfterCall,
|
|
52 TypeBuilderConsts.Priority.Normal - 100)]
|
|
53 public virtual int Prop { get { return 50; } }
|
|
54 }
|
|
55
|
|
56 [Test]
|
|
57 public void Test()
|
|
58 {
|
|
59 TestClass t = (TestClass)TypeAccessor.CreateInstance(typeof(TestClass));
|
|
60
|
|
61 int i2 = 2;
|
|
62 int i3;
|
|
63 decimal d4;
|
|
64
|
|
65 Assert.AreEqual(10, t.Test(1, ref i2, out i3, out d4));
|
|
66 Assert.AreEqual(0, t.TestNo());
|
|
67 Assert.AreEqual(60, t.Prop);
|
|
68 }
|
|
69 }
|
|
70 }
|