/
delegate.cs
94 lines (76 loc) · 1.96 KB
/
delegate.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
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
using System;
using System.Runtime.InteropServices;
class A {
public static bool b_cctor_run = false;
}
class B {
static B () {
A.b_cctor_run = true;
}
public static void method () {
}
}
delegate void DoIt ();
namespace Bah {
class Test {
[DllImport("cygwin1.dll", EntryPoint="puts", CharSet=CharSet.Ansi)]
public static extern int puts (string name);
delegate void SimpleDelegate ();
delegate string NotSimpleDelegate (int a);
delegate int AnotherDelegate (string s);
public int data;
static void F () {
Console.WriteLine ("Test.F from delegate");
}
public static string G (int a) {
if (a != 2)
throw new Exception ("Something went wrong in G");
return "G got: " + a.ToString ();
}
public string H (int a) {
if (a != 3)
throw new Exception ("Something went wrong in H");
return "H got: " + a.ToString () + " and " + data.ToString ();
}
public virtual void VF () {
Console.WriteLine ("Test.VF from delegate");
}
public Test () {
data = 5;
}
static int Main () {
// Check that creation of delegates do not runs the class cctor
DoIt doit = new DoIt (B.method);
if (A.b_cctor_run)
return 1;
Test test = new Test ();
SimpleDelegate d = new SimpleDelegate (F);
SimpleDelegate d1 = new SimpleDelegate (test.VF);
NotSimpleDelegate d2 = new NotSimpleDelegate (G);
NotSimpleDelegate d3 = new NotSimpleDelegate (test.H);
d ();
d1 ();
// we run G() and H() before and after using them as delegates
// to be sure we don't corrupt them.
G (2);
test.H (3);
Console.WriteLine (d2 (2));
Console.WriteLine (d3 (3));
G (2);
test.H (3);
if (d.Method.Name != "F")
return 1;
if (d3.Method == null)
return 1;
object [] args = {3};
Console.WriteLine (d3.DynamicInvoke (args));
AnotherDelegate d4 = new AnotherDelegate (puts);
if (d4.Method == null)
return 1;
Console.WriteLine (d4.Method);
Console.WriteLine (d4.Method.Name);
Console.WriteLine (d4.Method.DeclaringType);
return 0;
}
}
}