/
generic-delegate2.2.cs
123 lines (97 loc) · 2.85 KB
/
generic-delegate2.2.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
using System;
using System.Reflection;
/*
public delegate object ArrDel (int i);
public class Gen<T> {
public object newArr (int i) {
return new T [i];
}
public ArrDel newDel () {
return new ArrDel (this.newArr);
}
}
public delegate object DelObj (object g, int i);
public delegate object DelStr (object g, int i);
*/
public class main {
public static int work () {
Gen<string> gs = new Gen<string> ();
Gen<object> go = new Gen<object> ();
MethodInfo miObj = typeof (Gen<object>).GetMethod ("newArr", BindingFlags.Public | BindingFlags.Instance);
MethodInfo miStr = typeof (Gen<string>).GetMethod ("newArr", BindingFlags.Public | BindingFlags.Instance);
if (miObj == miStr) {
Console.WriteLine ("methods equal");
return 1;
}
ObjArrDel oad = go.newObjDel ();
StrArrDel sad = gs.newStrDel ();
StrArrDel sad2 = (StrArrDel)Delegate.CreateDelegate (typeof (StrArrDel), null, miStr);
if (oad.Method != miObj) {
Console.WriteLine ("wrong object method");
if (oad.Method == miStr)
Console.WriteLine ("object method is string");
return 1;
}
if (sad.Method != miStr) {
Console.WriteLine ("wrong string method");
if (sad.Method == miObj)
Console.WriteLine ("string method is object");
else
return 1;
} else {
Console.WriteLine ("right string method");
}
if (sad2.Method != miStr) {
Console.WriteLine ("wrong string2 method");
if (sad2.Method == miObj)
Console.WriteLine ("string2 method is object");
return 1;
}
Console.WriteLine ("calling object del");
if (oad (go, 3).GetType () != typeof (object [])) {
Console.WriteLine ("not object array");
return 1;
}
Console.WriteLine ("calling string del");
if (sad (gs, 3).GetType () != typeof (string [])) {
Console.WriteLine ("not string array");
return 1;
}
Console.WriteLine ("calling string del2");
if (sad2 (gs, 3).GetType () != typeof (string [])) {
Console.WriteLine ("not string2 array");
return 1;
}
try {
StrArrDel sad3 = (StrArrDel)Delegate.CreateDelegate (typeof (StrArrDel), null, miObj);
Console.WriteLine ("object method for string delegate");
return 1;
} catch (ArgumentException) {
}
/*
DelObj delObj = (DelObj)Delegate.CreateDelegate (typeof (DelObj), null, miObj);
if (delObj (go, 3).GetType () != typeof (object []))
return 1;
DelStr delStr = (DelStr)Delegate.CreateDelegate (typeof (DelStr), null, miStr);
if (delStr (gs, 3).GetType () != typeof (string []))
return 1;
*/
/*
ArrDel ad = go.newDel ();
if (ad (3).GetType () != typeof (object []))
return 1;
ad = gs.newDel ();
if (ad (3).GetType () != typeof (string []))
return 1;
*/
Console.WriteLine ("done");
return 0;
}
public static int Main () {
Gen<string> gs = new Gen<string> ();
Gen<object> go = new Gen<object> ();
gs.newArr (3);
go.newArr (3);
return work ();
}
}