forked from Ourpalm/ILRuntime
-
Notifications
You must be signed in to change notification settings - Fork 0
/
GenericMethodTest.cs
521 lines (431 loc) · 13.8 KB
/
GenericMethodTest.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
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using LitJson;
using ILRuntimeTest.TestFramework;
using ILRuntimeTest.TestBase;
namespace TestCases
{
class GenericMethodTest
{
class GTest
{
public void SetValue<K, V>(Dictionary<K, V> value)
{
Console.WriteLine($"SetValue1,{value.Count}");
}
public void SetValue<K, V>(List<Dictionary<K, V>> value)
{
Console.WriteLine($"SetValue2,{value.Count}");
}
public void SetValue<K, V>(Dictionary<K, List<V>> value)
{
Console.WriteLine($"SetValue3,{value.Count}");
}
public T SeekUI<T>(T rootTrans, string path) where T : ClassInheritanceTest
{
return rootTrans;
}
public T SeekUI<T>(string path) where T : ClassInheritanceTest, new()
{
return SeekUI<T>(new T(), path);
}
public void SeekUI<T>(string path, out T control) where T : ClassInheritanceTest, new()
{
control = SeekUI<T>(path);
}
}
delegate void ACallback<T>(T t);
class AClass
{
public string MSG;
}
class GenericClass<T>
{
public T t;
}
public delegate void CallbackDelegate<T>(T response);
public class Command<T>
{
public T Decode()
{
var json = "{}";
return LitJson.JsonMapper.ToObject<T>(json);
}
}
public class OneCallbackData
{
public string Message = $"This is {nameof(OneCallbackData)}";
}
public class AnotherCallbackData
{
public string Message = $"This is {nameof(AnotherCallbackData)}";
}
class TClass
{
public void Output<T>(T d, ACallback<T> callback)
{
callback(d);
}
public void Test<T>(T d, ACallback<T> callback)
{
Output(d, callback);
}
public void DoSomething<T>(Command<T> command, CallbackDelegate<T> callback)
{
callback(command.Decode());
}
public void Test<T>(CallbackDelegate<T> callback)
{
DoSomething(new Command<T>(), callback);
}
}
public static void GenericMethodTest1()
{
GTest test = new GTest();
List<Dictionary<string, object>> content = new List<Dictionary<string, object>>();
Dictionary<string, object> dic = new Dictionary<string, object>();
dic.Add("key1", 1000);
content.Add(dic);
test.SetValue(content);
}
public static void GenericMethodTest2()
{
GTest test = new GTest();
var t2 = test.SeekUI<TestCls>("abse");
Console.WriteLine(t2.TestVal2.ToString());
}
public static void GenericMethodTest3()
{
var tclass = new TClass();
var a = new AClass() { MSG = "AClass" };
Console.WriteLine("Simple Type");
tclass.Test(a, param =>
{
Console.WriteLine($"param: {LitJson.JsonMapper.ToJson(param)}");
});
var ga = new GenericClass<AClass>() { t = a };
Console.WriteLine("Complex Type");
tclass.Test(ga, param =>
{
Console.WriteLine($"param: {LitJson.JsonMapper.ToJson(param)}");
});
}
public static void GenericMethodTest4()
{
var tclass = new TClass();
tclass.Test<OneCallbackData>(param =>
{
Console.WriteLine($"输出: {LitJson.JsonMapper.ToJson(param)}");
});
tclass.Test<AnotherCallbackData>(param =>
{
Console.WriteLine($"输出: {LitJson.JsonMapper.ToJson(param)}");
});
}
public static void GenericMethodTest5()
{
BugTest test = new BugTest();
test.Test();
}
public static void GenericMethodTest6()
{
int i = 0;
Foo(2, out i);
Console.WriteLine(i);
}
static void Foo<T>(T arr, out int i)
{
i = 1;
Console.WriteLine(arr.ToString());
}
public static void GenericMethodTest7()
{
Foo(new List<int> { }, Bar); //NullReferenceException: Object reference not set to an instance of an object (at Assets/ILRuntime/CLR/TypeSystem/ILType.cs:762)
}
public static int Bar(List<int> o)
{
return 0;
}
public static void Foo<T1, T2>(T1 t, Func<T1, T2> func)
{
}
public static void GenericMethodTest8()
{
Foo2(new object[][] { }, Bar2); //NullReferenceException: Object reference not set to an instance of an object (at Assets/ILRuntime/CLR/TypeSystem/ILType.cs:762)
}
public static object Bar2(object o)
{
return o;
}
public static void Foo2<T1, T2>(T1[] t, Func<T1, T2> func)
{
}
class BugTest
{
public class TestClass<T>
{
}
public void Test()
{
TestClass<int> r;
dict.TryGetValue(0, out r);
}
Dictionary<int, TestClass<int>> dict = new Dictionary<int, TestClass<int>>();
}
//====================分割线,复现流程============================
// 接口Disposer定义
public interface IDisposer
{
int Id { get; set; }
void Foo(object bar, int bar2, object bar3);
void Dispose();
}
// 实现IDiposer的类
public class TestInterface : IDisposer
{
public int Id { get; set; }
public void Foo(object bar, int bar2, object bar3)
{
Console.WriteLine(bar.ToString() + bar2 + bar3);
}
public void Dispose()
{
}
}
static T CreateInterface<T>() where T : IDisposer
{
var type = typeof(TestInterface);
var result = Activator.CreateInstance(type);
return (T)result;
}
static T Fetch<T>() where T : IDisposer
{
var t = CreateInterface<T>();
// ((IDisposer)t).Id = 111; // ??
t.Id = 111; // ??
t.Foo(123, 456, 789);
return t;
}
public static void GenericMethodTest9()
{
var obj = Fetch<TestInterface>();
Console.WriteLine(obj.ToString());
}
interface IBinderCollection<T>
{
}
class BinderCollection<T> : IBinderCollection<T>
{
public void BindToGList<TListItem>(object _list, Action<T, TListItem> _bindItemAction)
{
Console.WriteLine(_list.ToString());
}
}
public static void GenericMethodTest10()
{
BinderCollection<string> binder = new BinderCollection<string>();
binder.BindToGList<object>(123, (a, b) => { });
}
public class TestBind<T> where T : IComparable<T>
{
public T data;
public T Data
{
get { return data; }
set
{
if (null != data && data.CompareTo(value) == 0)
{
return;
}
data = value;
}
}
}
public class SubBind : TestBind<int>
{
}
public static void GenericMethodTest11()
{
SubBind bind = new SubBind();
bind.Data = 100;
Console.WriteLine("bind.data = " + bind.data);
}
public static void GenericMethodTest12()
{
GenericMethodTest12Sub(new int[] { 3 }, k => {
k = k + 2;//error;
k = k << 2;//error;
});
}
static void GenericMethodTest12Sub<T>(T[] a, Action<T> func)
{
func(a[0]);
}
public static void GenericMethodTest13()
{
GenericMethodTest13Sub<int>();
}
static void GenericMethodTest13Sub<T>()
{
var t = typeof(T[]);
Console.WriteLine(t.ToString());
}
public static void GenericExtensionMethod1Test1()
{
new ExtensionClass().Method1(v => { });
}
public static void GenericExtensionMethod1Test2()
{
new ExtensionClass().Method1((i, v) => { });
}
public static void GenericExtensionMethod2Test1()
{
new ExtensionClass().Method2(e => { });
}
public static void GenericExtensionMethod2Test2()
{
new ExtensionClass().Method2((i, e) => { });
}
public static void GenericExtensionMethod2Test3()
{
new ExtensionClass().Method2<ArgumentException>(e => { });
}
public static void GenericExtensionMethod2Test4()
{
new ExtensionClass().Method2<ArgumentException>((i, e) => { });
}
public static void GenericExtensionMethod2Test5()
{
new ExtensionClass<int>().Method2(e => { });
}
public static void GenericExtensionMethod2Test6()
{
new ExtensionClass<int>().Method2((i, e) => { });
}
public static void GenericExtensionMethod2Test7()
{
new ExtensionClass<int>().Method2<int, ArgumentException>(e => { });
}
public static void GenericExtensionMethod2Test8()
{
new ExtensionClass<int>().Method2<int, ArgumentException>((i, e) => { });
}
public static void GenericExtensionMethod3Test1()
{
new ExtensionClass().Method3(null);
}
public static void GenericExtensionMethod3Test2()
{
new ExtensionClass<int>().Method3(null);
}
public static void GenericExtensionMethod3Test3()
{
new SubExtensionClass().Method3(null);
}
public static void GenericExtensionMethod3Test4()
{
new SubExtensionClass<int>().Method3(null);
}
public static void GenericExtensionMethod3Test5()
{
new ExtensionClass().Method3(new ArgumentException());
}
public static void GenericExtensionMethod3Test6()
{
new ExtensionClass<int>().Method3(new ArgumentException());
}
public static void GenericExtensionMethod3Test7()
{
new SubExtensionClass<int>().Method3(new ArgumentException());
}
public static void GenericStaticMethodTest1()
{
StaticGenericMethods.StaticMethod(() => { });
}
public static void GenericStaticMethodTest2()
{
StaticGenericMethods.StaticMethod(i => { });
}
public static void GenericStaticMethodTest3()
{
StaticGenericMethods.StaticMethod(() => 1);
}
public static void GenericStaticMethodTest4()
{
StaticGenericMethods.StaticMethod(i => 1);
}
public static void GenericStaticMethodTest5()
{
StaticGenericMethods.StaticMethod(async () => await Task.Delay(1));
}
public static void GenericStaticMethodTest6()
{
StaticGenericMethods.StaticMethod(async i => await Task.Delay(1));
}
public static void GenericStaticMethodTest7()
{
StaticGenericMethods.StaticMethod(async () => await Task.FromResult(1));
}
public static void GenericStaticMethodTest8()
{
StaticGenericMethods.StaticMethod(async i => await Task.FromResult(1));
}
public static void GenericStaticMethodTest9()
{
GenericStaticMethodTest_9("","",(MethodTest9_A[] arr) => {
});
}
class MethodTest9_A { };
public static void GenericStaticMethodTest_9<T>(string bundle, string assetname, Action<T[]> callback)
{
}
static void TestMethod<T>(Action<GenericClass<T>> enter)
{
GenericClass<T> ins = new GenericClass<T>();
enter(ins);
}
public static void GenericStaticMethodTest10()
{
TestMethod<int>((ins) =>
{
Console.WriteLine(ins.t);
});
}
class ClassA<T>
{
}
class ClassB<TT>
{
public void TestMethod(out ClassA<TT> enter)
{
enter = null;
}
}
public static void GenericStaticMethodTest11()
{
ClassB<int> a = new ClassB<int>();
ClassB<string> b = new ClassB<string>();
ClassA<int> aa = new ClassA<int>();
ClassA<string> bb = new ClassA<string>();
a.TestMethod(out aa);
b.TestMethod(out bb);
}
public static void GenericStaticMethodTest12()
{
ILRuntimeTest.TestBase.StaticGenericMethods.Method("");
}
public static void GenericStaticMethodTest13()
{
GenericStaticMethodTest13Sub<int[]>();
}
static void GenericStaticMethodTest13Sub<T>()
{
var t = typeof(T);//出错
Console.WriteLine(t);
}
}
}