7c86a174f13fa447174a1032b5e706fd47649905
[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
29         public virtual void VF () {
30                 Console.WriteLine ("Test.VF from delegate");
31         }
32         
33         public Test () {
34                 data = 5;
35         }
36         static int Main () {
37                 Test test = new Test ();
38                 SimpleDelegate d = new SimpleDelegate (F);
39                 SimpleDelegate d1 = new SimpleDelegate (test.VF);
40                 NotSimpleDelegate d2 = new NotSimpleDelegate (G);
41                 NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
42                 d ();
43                 d1 ();
44                 // we run G() and H() before and after using them as delegates
45                 // to be sure we don't corrupt them.
46                 G (2);
47                 test.H (3);
48                 Console.WriteLine (d2 (2));
49                 Console.WriteLine (d3 (3));
50                 G (2);
51                 test.H (3);
52
53                 if (d.Method.Name != "F")
54                         return 1;
55
56                 if (d3.Method == null)
57                         return 1;
58                 
59                 object [] args = {3};
60                 Console.WriteLine (d3.DynamicInvoke (args));
61
62                 AnotherDelegate d4 = new AnotherDelegate (puts);
63                 if (d4.Method == null)
64                         return 1;
65
66                 Console.WriteLine (d4.Method);
67                 Console.WriteLine (d4.Method.Name);
68                 Console.WriteLine (d4.Method.DeclaringType);
69                 
70                 return 0;
71
72
73         }
74 }
75 }