* roottypes.cs: Rename from tree.cs.
[mono.git] / mono / tests / invoke.cs
1 using System;
2 using System.Reflection;
3
4 class Test {
5         
6         public struct SimpleStruct {
7                 public bool a;
8                 public bool b;
9
10                 public SimpleStruct (bool arg) {
11                         a = arg;
12                         b = false;
13                 }
14         }
15         
16         static void Test2 () {
17                 Console.WriteLine ("Test2 called");
18         }
19         
20         public static SimpleStruct Test1 (SimpleStruct ss) {
21                 Console.WriteLine ("Test1 called " + ss.a + " " + ss.b);
22                 SimpleStruct res = new SimpleStruct ();
23                 res.a = !ss.a;
24                 res.b = !ss.b;
25                 return res;
26         }
27
28         public static void Foo(ref int x, ref int y)
29         {
30                 x = 20;
31                 y = 30;
32         }
33         
34         static int Main () {
35                 Type t = typeof (Test);
36
37                 MethodInfo m2 = t.GetMethod ("Test2");
38                 if (m2 != null)
39                         return 1;
40
41                 MethodInfo m1 = t.GetMethod ("Test1");
42                 if (m1 == null)
43                         return 1;
44
45                 object [] args = new object [1];
46                 SimpleStruct ss = new SimpleStruct ();
47                 ss.a = true;
48                 ss.b = false;
49                 args [0] = ss;
50                 
51                 SimpleStruct res = (SimpleStruct)m1.Invoke (null, args);
52
53                 if (res.a == true)
54                         return 1;
55                 if (res.b == false)
56                         return 1;
57
58                 // Test that the objects for byref valuetype arguments are 
59                 // automatically created
60                 MethodInfo m3 = typeof(Test).GetMethod("Foo");
61                 
62                 args = new object[2];
63                 
64                 m3.Invoke(null, args);
65
66                 if ((((int)(args [0])) != 20) || (((int)(args [1])) != 30))
67                         return 2;
68
69                 // Test the return value from  ConstructorInfo.Invoke when a precreated
70                 // valuetype is used.
71                 ConstructorInfo ci = typeof (SimpleStruct).GetConstructor (new Type [] { typeof (bool) });
72                 SimpleStruct res2 = (SimpleStruct)ci.Invoke (ss, new object [] { false });
73         
74                 return 0;
75         }
76 }