2005-02-14 Sebastien Pouliot <sebastien@ximian.com>
[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         
11         static void Test2 () {
12                 Console.WriteLine ("Test2 called");
13         }
14         
15         public static SimpleStruct Test1 (SimpleStruct ss) {
16                 Console.WriteLine ("Test1 called " + ss.a + " " + ss.b);
17                 SimpleStruct res = new SimpleStruct ();
18                 res.a = !ss.a;
19                 res.b = !ss.b;
20                 return res;
21         }
22
23         public static void Foo(ref int x, ref int y)
24         {
25                 x = 20;
26                 y = 30;
27         }
28         
29         static int Main () {
30                 Type t = typeof (Test);
31
32                 MethodInfo m2 = t.GetMethod ("Test2");
33                 if (m2 != null)
34                         return 1;
35
36                 MethodInfo m1 = t.GetMethod ("Test1");
37                 if (m1 == null)
38                         return 1;
39
40                 object [] args = new object [1];
41                 SimpleStruct ss = new SimpleStruct ();
42                 ss.a = true;
43                 ss.b = false;
44                 args [0] = ss;
45                 
46                 SimpleStruct res = (SimpleStruct)m1.Invoke (null, args);
47
48                 if (res.a == true)
49                         return 1;
50                 if (res.b == false)
51                         return 1;
52
53                 // Test that the objects for byref valuetype arguments are 
54                 // automatically created
55                 MethodInfo m3 = typeof(Test).GetMethod("Foo");
56                 
57                 args = new object[2];
58                 
59                 m3.Invoke(null, args);
60
61                 if ((((int)(args [0])) != 20) || (((int)(args [1])) != 30))
62                         return 2;
63         
64                 return 0;
65         }
66 }