-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathDelegateCastTest.cs
140 lines (101 loc) · 3.31 KB
/
DelegateCastTest.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
134
135
136
137
138
139
140
using System;
namespace TestCases
{
public class CastSubObj
{
public int Value;
}
public static class CastSubObjExtendMethod
{
public static CastSubObj2 AddValue(this CastSubObj cast, DelegateCast<CastSubObj> arg)
{
Console.WriteLine(arg);
return new CastSubObj2();
}
}
public class CastSubObj2
{
public int Value;
public void AddValue(int value)
{
this.Value += value;
}
}
public class DelegateCast<T>
{
}
public class DelegateCastTest
{
public delegate CastSubObj2 Cast_Func1();
public delegate CastSubObj2 Cast_Func2(DelegateCast<CastSubObj> a);
public delegate CastSubObj2 Cast_Func3(DelegateCast<CastSubObj> a);
public delegate object Cast_Func5(CastSubObj2 A);
public delegate void Cast_Action4(DelegateCast<CastSubObj> a);
public static void DelegateCastTest1()
{
Cast_Func3 afunc = new CastSubObj().AddValue;
object obj = afunc;
Func<DelegateCast<CastSubObj>, CastSubObj> action2 = obj as Func<DelegateCast<CastSubObj>, CastSubObj>;
Func<DelegateCast<CastSubObj>, CastSubObj2> action3 = obj as Func<DelegateCast<CastSubObj>, CastSubObj2>;
var action4 = obj as Cast_Func2;
var action5 = obj as Action<DelegateCast<CastSubObj>, CastSubObj2>;
if (action2 != null)
{
throw new Exception();
}
if (action3 == null)
{
throw new Exception();
}
if (action4 == null)
{
throw new Exception();
}
if (action5 != null)
{
throw new Exception();
}
}
public static void DelegateCastTest2()
{
Cast_Func2 afunc = (x) => { return null; };
object obj = afunc;
Func<DelegateCast<CastSubObj>, CastSubObj> action2 = obj as Func<DelegateCast<CastSubObj>, CastSubObj>;
Action<DelegateCast<CastSubObj>, CastSubObj> action3 = obj as Action<DelegateCast<CastSubObj>, CastSubObj>;
Action action4 = obj as Action;
var action5 = obj as Action<DelegateCast<CastSubObj>, CastSubObj>;
var action7 = obj as Func<DelegateCast<CastSubObj>, CastSubObj2>;
if (action2 != null)
{
throw new Exception();
}
if (action3 != null)
{
throw new Exception();
}
if (action4 != null)
{
throw new Exception();
}
if (action5 != null)
{
throw new Exception();
}
if (action7 == null)
{
throw new Exception();
}
}
[ILRuntimeTest.ILRuntimeTest(IsToDo = true)]
public static void DelegateCastTest3()
{
Cast_Func2 afunc = (x) => { return null; };
object obj = afunc;
var action6 = (Action<DelegateCast<CastSubObj>, CastSubObj>)obj;
if (action6 != null)//TODO:cast后应为空
{
throw new Exception();
}
}
}
}