2002-03-28 Dick Porter <dick@ximian.com>
[mono.git] / mono / tests / delegate.cs
1 using System;
2 using System.Runtime.InteropServices;
3
4 namespace Bah {
5 class Test {
6         [DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
7         public static extern int puts (string name);
8
9         delegate void SimpleDelegate ();
10         delegate string NotSimpleDelegate (int a);
11         delegate int AnotherDelegate (string s);
12         
13         public int data;
14         
15         static void F () {
16                 Console.WriteLine ("Test.F from delegate");
17         }
18         public static string G (int a) {
19                 if (a != 2)
20                         throw new Exception ("Something went wrong in G");
21                 return "G got: " + a.ToString ();
22         }
23         public string H (int a) {
24                 if (a != 3)
25                         throw new Exception ("Something went wrong in H");
26                 return "H got: " + a.ToString () + " and " + data.ToString ();
27         }
28         public Test () {
29                 data = 5;
30         }
31         static int Main () {
32                 Test test = new Test ();
33                 SimpleDelegate d = new SimpleDelegate (F);
34                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
35                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
36                 d ();
37                 // we run G() and H() before and after using them as delegates
38                 // to be sure we don't corrupt them.
39                 G (2);
40                 test.H (3);
41                 Console.WriteLine (d2 (2));
42                 Console.WriteLine (d3 (3));
43                 G (2);
44                 test.H (3);
45
46                 if (d.Method.Name != "F")
47                         return 1;
48
49                 if (d3.Method == null)
50                         return 1;
51                 
52                 object [] args = {3};
53                 Console.WriteLine (d3.DynamicInvoke (args));
54
55                 AnotherDelegate d4 = new AnotherDelegate (puts);
56                 if (d4.Method == null)
57                         return 1;
58
59                 Console.WriteLine (d4.Method);
60                 Console.WriteLine (d4.Method.Name);
61                 Console.WriteLine (d4.Method.DeclaringType);
62                 
63                 return 0;
64
65
66         }
67 }
68 }