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