-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathDelegateExtTest.cs
133 lines (112 loc) · 3.47 KB
/
DelegateExtTest.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
124
125
126
127
128
129
130
131
132
133
using System;
namespace TestCases
{
public class DelegateExtObj
{
public int Value;
public void AddValue(int value)
{
this.Value += value;
}
}
public static class DelegateExtObjMethod
{
public static void IntTest(this DelegateExtObj obj, int a)
{
obj.AddValue(1);
Console.WriteLine(obj + " dele a=" + a);
}
public static void IntTest2(this DelegateExtObj obj, int a)
{
obj.AddValue(123);
Console.WriteLine(obj + " dele2 a=" + obj.Value);
}
public static int IntTest3(this DelegateExtObj obj, int a)
{
Console.WriteLine(obj + " dele3 a=" + a);
return a + 100;
}
public static string Void(this DelegateExtObj obj,string str)
{
Console.WriteLine(obj + " Void");
return "Void"+str+"x";
}
public static string Extend(this DelegateExtObj obj,string str)
{
if (obj == null)
throw new Exception();
return "Extend" + str;
}
}
public class DelegateExtTest
{
static TestDelegate testDele;
public static void DelegateExtTest01()
{
var obj = new DelegateExtObj();
ILRuntimeTest.TestFramework.DelegateTest.IntDelegateTest += obj.IntTest;
ILRuntimeTest.TestFramework.DelegateTest.IntDelegateTest += obj.IntTest2;
ILRuntimeTest.TestFramework.DelegateTest.IntDelegateTest(123);
Func<DelegateExtObj,string,string> func = DelegateExtObjMethod.Extend;
var ret = func.Invoke(obj,"1");
if(!ret.Equals("Extend1"))
throw new Exception();
}
public static void DelegateExtTest02()
{
var obj = new DelegateExtObj();
Action<int> a = null;
a += obj.IntTest;
a += obj.IntTest2;
DelegateTestCls cls = new DelegateTestCls(1000);
a += cls.IntTest;
a += cls.IntTest2;
a += (i) =>
{
Console.WriteLine("lambda a=" + i);
};
a.Invoke(124);
Console.WriteLine("obj Value=" + obj.Value);
}
public static void DelegateExtTest03()
{
var obj = new DelegateExtObj();
Func<string,string> a = null;
a += obj.Void;
a += obj.Extend;
Console.WriteLine("a=" + a("xxzz"));
}
public static void DelegateExtTest04()
{
var obj = new DelegateExtObj();
Func<string> a = null;
var o1 = new DelegateTestCls(11);
a += o1.GetString;
Console.WriteLine("no extend method lambda a=" + a());
}
class DelegateTestCls : DelegateTestClsBase
{
public DelegateTestCls(int b)
{
this.b = b;
}
public string GetString()
{
return "x";
}
}
class DelegateTestClsBase
{
protected int b;
public virtual void IntTest(int a)
{
Console.WriteLine("dele3base a=" + (a + b));
}
public virtual void IntTest2(int a)
{
Console.WriteLine("dele4 a=" + (a + b));
}
}
delegate int TestDelegate(int b);
}
}