comparison HowTo/Aspects/AsyncAspect.cs @ 0:f990fcb411a9

Копия текущей версии из github
author cin
date Thu, 27 Mar 2014 21:46:09 +0400
parents
children
comparison
equal deleted inserted replaced
-1:000000000000 0:f990fcb411a9
1 using System;
2 using System.Threading;
3
4 using NUnit.Framework;
5
6 using BLToolkit.Aspects;
7 using BLToolkit.Reflection;
8
9 namespace HowTo.Aspects
10 {
11 public /*[a]*/abstract/*[/a]*/ class AsyncTestObject
12 {
13 // This is a member we will call asynchronously.
14 //
15 public /*[a]*/int/*[/a]*/ /*[a]*/Test/*[/a]*/(/*[a]*/int intVal, string strVal/*[/a]*/)
16 {
17 Thread.Sleep(200);
18 return intVal;
19 }
20
21 // Begin async method should take the same parameter list as the Test method and return IAsyncResult.
22 // Two additional parameters can be provided: AsyncCallback and state object.
23 // 'Begin' prefix is a part of the default naming convention.
24 //
25 [/*[a]*/Async/*[/a]*/] public abstract /*[a]*/IAsyncResult/*[/a]*/ /*[a]*/BeginTest/*[/a]*/(/*[a]*/int intVal, string strVal/*[/a]*/);
26 [/*[a]*/Async/*[/a]*/] public abstract /*[a]*/IAsyncResult/*[/a]*/ /*[a]*/BeginTest/*[/a]*/(/*[a]*/int intVal, string strVal/*[/a]*/, /*[a]*/AsyncCallback/*[/a]*/ callback);
27 [/*[a]*/Async/*[/a]*/] public abstract /*[a]*/IAsyncResult/*[/a]*/ /*[a]*/BeginTest/*[/a]*/(/*[a]*/int intVal, string strVal/*[/a]*/, /*[a]*/AsyncCallback/*[/a]*/ callback, /*[a]*/object/*[/a]*/ state);
28
29 // End async method should take IAsyncResult and return the same type as the Test method.
30 // 'End' prefix is a part of the default naming convention.
31 //
32 [/*[a]*/Async/*[/a]*/] public abstract /*[a]*/int/*[/a]*/ /*[a]*/EndTest/*[/a]*/ (/*[a]*/IAsyncResult/*[/a]*/ asyncResult);
33
34 // Begin/End naming convention is not required. You can use any name
35 // if you provide the target method name as a parameter of the Async attribute.
36 //
37 [/*[a]*/Async/*[/a]*/("Test")]
38 public abstract /*[a]*/IAsyncResult/*[/a]*/ AnyName(/*[a]*/int intVal/*[/a]*/, /*[a]*/string strVal/*[/a]*/, /*[a]*/AsyncCallback/*[/a]*/ callback, /*[a]*/object/*[/a]*/ state);
39
40 // Here we provide the parameter list along with the method name.
41 //
42 [/*[a]*/Async/*[/a]*/("Test", typeof(int), typeof(string))]
43 public abstract /*[a]*/int/*[/a]*/ AnyName(/*[a]*/IAsyncResult/*[/a]*/ asyncResult);
44 }
45
46 [TestFixture]
47 public class AsyncAspectTest
48 {
49 [Test]
50 public void AsyncTest()
51 {
52 AsyncTestObject o = /*[a]*/TypeAccessor/*[/a]*/<AsyncTestObject>.CreateInstance();
53
54 IAsyncResult ar = o./*[a]*/BeginTest/*[/a]*/(1, "10");
55 Assert.AreEqual(1, o./*[a]*/EndTest/*[/a]*/(ar));
56 }
57
58 private static void /*[a]*/CallBack/*[/a]*/(IAsyncResult ar)
59 {
60 Console.WriteLine("Callback");
61
62 AsyncTestObject o = (AsyncTestObject)ar.AsyncState;
63 o.EndTest(ar);
64 }
65
66 [Test]
67 public void CallbackTest()
68 {
69 AsyncTestObject o = TypeAccessor<AsyncTestObject>.CreateInstance();
70
71 o.BeginTest(2, null, /*[a]*/CallBack/*[/a]*/, /*[a]*/o/*[/a]*/);
72 }
73
74 [Test]
75 public void AnyNameTest()
76 {
77 AsyncTestObject o = TypeAccessor<AsyncTestObject>.CreateInstance();
78
79 IAsyncResult ar = o./*[a]*/AnyName/*[/a]*/(2, null, null, null);
80 Assert.AreEqual(2, o./*[a]*/AnyName/*[/a]*/(ar));
81 }
82 }
83 }