copying the latest Sys.Web.Services from trunk.
[mono.git] / mono / tests / delegate.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 class A {
5         public static bool b_cctor_run = false;
6 }
7
8 class B {
9         static B () {
10                 A.b_cctor_run = true;
11         }
12         public static void method () {
13         }
14 }
15
16 delegate void DoIt ();
17
18 namespace Bah {
19 class Test {
20         [DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
21         public static extern int puts (string name);
22
23         delegate void SimpleDelegate ();
24         delegate string NotSimpleDelegate (int a);
25         delegate int AnotherDelegate (string s);
26
27         delegate string StringDelegate (); 
28         
29         public int data;
30         
31         static void F () {
32                 Console.WriteLine ("Test.F from delegate");
33         }
34         public static string G (int a) {
35                 if (a != 2)
36                         throw new Exception ("Something went wrong in G");
37                 return "G got: " + a.ToString ();
38         }
39         public string H (int a) {
40                 if (a != 3)
41                         throw new Exception ("Something went wrong in H");
42                 return "H got: " + a.ToString () + " and " + data.ToString ();
43         }
44
45         public virtual void VF () {
46                 Console.WriteLine ("Test.VF from delegate");
47         }
48         
49         public Test () {
50                 data = 5;
51         }
52         static int Main () {
53                 // Check that creation of delegates do not runs the class cctor
54                 DoIt doit = new DoIt (B.method);
55                 if (A.b_cctor_run)
56                         return 1;
57
58                 Test test = new Test ();
59                 SimpleDelegate d = new SimpleDelegate (F);
60                 SimpleDelegate d1 = new SimpleDelegate (test.VF);
61                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
62                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
63                 d ();
64                 d1 ();
65                 // we run G() and H() before and after using them as delegates
66                 // to be sure we don't corrupt them.
67                 G (2);
68                 test.H (3);
69                 Console.WriteLine (d2 (2));
70                 Console.WriteLine (d3 (3));
71                 G (2);
72                 test.H (3);
73
74                 if (d.Method.Name != "F")
75                         return 1;
76
77                 if (d3.Method == null)
78                         return 1;
79                 
80                 object [] args = {3};
81                 Console.WriteLine (d3.DynamicInvoke (args));
82
83                 AnotherDelegate d4 = new AnotherDelegate (puts);
84                 if (d4.Method == null)
85                         return 1;
86
87                 Console.WriteLine (d4.Method);
88                 Console.WriteLine (d4.Method.Name);
89                 Console.WriteLine (d4.Method.DeclaringType);
90
91                 // Check unboxing of this argument
92                 int x = 10;
93                 StringDelegate d5 = new StringDelegate (x.ToString);
94                 if (d5 () != "10")
95                         return 1;
96                 
97                 return 0;
98
99
100         }
101 }
102 }