2009-03-04 Zoltan Varga <vargaz@gmail.com>
[mono-debugger.git] / mono / tests / invoke.cs
blob9add9a0bc0aaa7758ffa40336ba2168651d575e0
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 ci.Invoke (ss, new object [] { false });
74 // Test invoking of the array Get/Set methods
75 string[,] arr = new string [10, 10];
77 arr.GetType ().GetMethod ("Set").Invoke (arr, new object [] { 1, 1, "FOO" });
78 string s = (string)arr.GetType ().GetMethod ("Get").Invoke (arr, new object [] { 1, 1 });
79 if (s != "FOO")
80 return 3;
82 // Test the sharing of runtime invoke wrappers for string ctors
83 typeof (string).GetConstructor (new Type [] { typeof (char[]) }).Invoke (null, new object [] { new char [] { 'a', 'b', 'c' } });
85 typeof (Assembly).GetMethod ("GetType", new Type [] { typeof (string), }).Invoke (typeof (int).Assembly, new object [] { "A" });
87 return 0;