-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathGenericMethodTest.cs
741 lines (612 loc) · 19.9 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
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
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
{
public static class TestGenericExtensionExtension
{
public class Test1<T>
{
}
public class Test2<T>
{
}
public static void CreateCollector<T>(this Test1<T> arg1, Test2<T>[] arg2)
{
}
}
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;
Console.WriteLine($"k={k}");
});
}
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 GenericMethodTest14()
{
Dictionary<int, List<int>> _notSelDict = new Dictionary<int, List<int>>();
var notSelGuids = _notSelDict.SelectMany(dict => dict.Value).ToList();
Console.WriteLine(notSelGuids.Count.ToString());
}
class Creater<T>
{
public bool Test(T me, T other)
{
var idiot1 = me as Man;
var idiot2 = other as Man;
return idiot1.Test(idiot2);
}
}
class Man
{
int a;
public Man(int a)
{
this.a = a;
}
public bool Test(Man other) => a == other.a;
}
static void Test<T>(Creater<T> creater) where T : Man
{
var m1 = new Man(33);
var m2 = new Man(33);
T t1 = (T)m1;
T t2 = (T)m2;
//传入创建的create有问题
if (creater.Test(t1, t2)) // Exception NullReference
Console.WriteLine(true);
else
Console.WriteLine(false);
}
static Creater<T> TestCreate<T>() => new Creater<T>();
// 测试用例
public static void GenericMethodTest15()
{
var creater = TestCreate<Man>();
Test(creater);
}
public static void GenericMethodTest16()
{
var testa = new TestA<int>();
testa.TestStatic(); //输出了‘arr’ 然后报错 调用了泛型数组的方法,正常应该调用泛型接口的方法
}
public static void GenericMethodTest17()
{
Foo3<object>(typeof(GenericMethodTest));
}
public static void GenericMethodTest18()
{
float[] val = TestStaticGenericClassField<float[]>.GetValue("key");
Console.WriteLine($"=======TestStaticGenericClassField<float[]>.GetValue: {val}");
var val2 = TestStaticGenericClassField<float>.GetValue("key");
Console.WriteLine($"=======TestStaticGenericClassField<float>.GetValue: {val2}");
}
static class TestStaticGenericClassField<T>
{
public static readonly Dictionary<string, T> Dict = new Dictionary<string, T>();
public static T GetValue(string key, T def = default(T))
{
if (Dict.TryGetValue(key, out var res))
{
return res;
}
return def;
}
}
static void Foo3<T>(out T value) where T : class
{
value = null;
throw new Exception();
}
static void Foo3<T>(Type type) where T : class
{
}
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 GenericExtensionMethod3Test8()
{
var t1 = new TestGenericExtensionExtension.Test1<int>();
var t2 = new TestGenericExtensionExtension.Test2<int>[] { new TestGenericExtensionExtension.Test2<int>() };
t1.CreateCollector(t2);
}
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);
}
public static void GenericStaticMethodTest14()
{
var e2 = GenericStaticMethodTest14Sub<EnumTest.TestEnum>();
Console.WriteLine(e2);
}
static T GenericStaticMethodTest14Sub<T>()
{
return default(T);
}
public static void GenericStaticMethodTest15()
{
var e2 = GenericStaticMethodTest14Sub<TestVector3>();
Console.WriteLine(e2);
}
public static void GenericStaticMethodTest16()
{
var e2 = GenericStaticMethodTest14Sub<TestVector3NoBinding>();
Console.WriteLine(e2);
}
public static void GenericStaticMethodTest17()
{
var e2 = GenericStaticMethodTest14Sub<int>();
Console.WriteLine(e2);
}
public static void GenericStaticMethodTest18()
{
var e2 = GenericStaticMethodTest14Sub<Vector3>();
Console.WriteLine(e2);
}
public static void GenericTest()
{
Dictionary<int, string[][]> dic = new Dictionary<int, string[][]>();
dic[0] = new string[5][];
foreach(var i in dic)
{
if (i.Key != 0 || i.Value.Length != 5)
throw new Exception();
}
}
public static void GenericStaticMethodTest19()
{
GenericStaticMethodTest19Sub(new testConstrainsA());
GenericStaticMethodTest19Sub2(new testConstrainsA());
}
class testConstrainsA
{
}
static void GenericStaticMethodTest19Sub<T>(T a) where T : testConstrainsA
{
object test = null;
var testB = test as T; // 此行报空指针
Console.WriteLine(testB);
}
static void GenericStaticMethodTest19Sub2<T>(T a) where T : testConstrainsA
{
var testB = a as T; // 此行报空指针
Console.WriteLine(testB);
}
}
interface ITestA<T> //泛型接口
{
void Test();
}
class TestA<T> : ITestA<T>
{
public void Test()
{
Console.WriteLine("TestA impl");
}
}
static class TestStaticClassGenericMethod //扩展方法静态类 两个方法同名
{
public static void TestStatic<T>(this T[] arr) //泛型数组
{
Console.WriteLine("arr");
Console.WriteLine(arr.Length);
throw new Exception("Wrong method");
}
public static void TestStatic<T>(this ITestA<T> test)//泛型接口
{
Console.WriteLine("ITestA");
test.Test();
}
}
}