2007-03-28 Chris Toshok <toshok@ximian.com>
[mono-project.git] / mono / tests / invoke.cs
blob387f0ecb95ec18b4f9de2261dd6b8de7570ba5f3
1 using System;
2 using System.Reflection;
4 class Test {
6 public struct SimpleStruct {
7 public bool a;
8 public bool b;
10 public SimpleStruct (bool arg) {
11 a = arg;
12 b = false;
16 static void Test2 () {
17 Console.WriteLine ("Test2 called");
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;
28 public static void Foo(ref int x, ref int y)
30 x = 20;
31 y = 30;
34 static int Main () {
35 Type t = typeof (Test);
37 MethodInfo m2 = t.GetMethod ("Test2");
38 if (m2 != null)
39 return 1;
41 MethodInfo m1 = t.GetMethod ("Test1");
42 if (m1 == null)
43 return 1;
45 object [] args = new object [1];
46 SimpleStruct ss = new SimpleStruct ();
47 ss.a = true;
48 ss.b = false;
49 args [0] = ss;
51 SimpleStruct res = (SimpleStruct)m1.Invoke (null, args);
53 if (res.a == true)
54 return 1;
55 if (res.b == false)
56 return 1;
58 // Test that the objects for byref valuetype arguments are
59 // automatically created
60 MethodInfo m3 = typeof(Test).GetMethod("Foo");
62 args = new object[2];
64 m3.Invoke(null, args);
66 if ((((int)(args [0])) != 20) || (((int)(args [1])) != 30))
67 return 2;
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 });
74 return 0;