-
Notifications
You must be signed in to change notification settings - Fork 659
/
Copy pathInheritanceTest.cs
1033 lines (876 loc) · 25.3 KB
/
InheritanceTest.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
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ILRuntime.Runtime;
using ILRuntimeTest.TestFramework;
namespace TestCases
{ public interface InterfaceTest2
{
void TestVirtual();
void TestAbstract();
void TestField();
}
public class InheritanceTest
{
interface ITest
{
void TestMethod();
}
public class TestA : ITest
{
void ITest.TestMethod()//显式实现接口方法
{
Console.WriteLine("方法A");
}
}
public class TestB : ITest
{
void ITest.TestMethod()//显式实现接口方法
{
Console.WriteLine("方法A");
}
public void TestMethod()
{
Console.WriteLine("方法B");
}
}
public static void InheritanceTest01()
{
TestCls cls = new TestCls();
TestCls2 cls2 = new TestCls2();
Console.WriteLine("Test invoking from sub type...");
Console.WriteLine(cls.ToString());
cls.TestAbstract();
cls.TestVirtual();
cls.TestField();
Console.WriteLine(cls2.ToString());
cls2.TestAbstract();
cls2.TestVirtual();
cls2.TestField();
cls2.AA1();
Console.WriteLine("----------------------------------");
Console.WriteLine("----------------------------------");
Test01Sub(cls);
Test01Sub(cls2);
Console.WriteLine("TestCls.TestVal2 = " + cls.TestVal2);
ClassInheritanceTest.Test3(cls);
ClassInheritanceTest.Test3(cls2);
}
public static void InheritanceTest_Interface()
{
TestCls3 cls3 = new TestCls3();
Console.WriteLine(cls3.ToString());
cls3.TestAbstract();
((InterfaceTest2)cls3).TestVirtual();
cls3.TestField();
}
public static void InheritanceTest_Interface2()
{
ITest b = new TestB();
b.TestMethod();//此处输入结果为 方法B
ITest a = new TestA();
a.TestMethod();//发生异常
}
static void Test01Sub(ClassInheritanceTest cls)
{
Console.WriteLine("Test invoking from base type...");
cls.TestAbstract();
cls.TestVirtual();
cls.TestField();
}
public static void InheritanceTest02()
{
GenericTestCls<TestCls> cls = new GenericTestCls<TestCls>();
GenericTestCls<TestCls2> cls2 = new GenericTestCls<TestCls2>();
cls.DoTest();
cls2.DoTest();
}
public static void InheritanceTest03()
{
List<TestCls> list = new List<TestCls>();
TestCls a = new TestCls();
a.TestVal2 = 243;
list.Add(a);
Console.WriteLine(list[0].TestVal2.ToString());
}
public static void InheritanceTest04()
{
List<GenericTestCls<TestCls>> list = new List<GenericTestCls<TestCls>>();
list.Add(new GenericTestCls<TestCls>());
list[0].DoTest();
}
public static void InheritanceTest05()
{
TestCls4 cls = new TestCases.TestCls4();
cls.TestVirtual();
}
public static void InheritanceTest07()
{
TestClass2 cls = new TestCls5();
Console.WriteLine(cls);
cls.VMethod1();
int val = 0;
cls.VMethod3(ref val);
if (val != 11)
throw new Exception($"{val} != 11");
float val2 = cls.AbMethod2(val);
if (val2 != 12.2f)
throw new Exception($"{val2} != 12.1f");
cls = new TestCls6();
val = 0;
cls.VMethod3(ref val);
if (val != 21)
throw new Exception($"{val} != 21");
val2 = cls.AbMethod2(val);
if (val2 != 24.2f)
throw new Exception($"{val2} != 23.2f");
}
public static void InheritanceTest08()
{
using (AABase obj = new AABase())
{
obj.R = 1;
obj.R2 = 2;
var m = typeof(InheritanceTest).GetMethod("InheritanceTest08_Sub");
m.Invoke(null, new object[] { obj });
}
}
static void InheritanceTest08_Sub(AABase obj)
{
var k = obj.R;
var l = obj.R2;
Console.WriteLine(string.Format("{0},{1}", k, l));
}
public static void InheritanceTest09()
{
ClassInheritanceTest a = new TestCls();
ClassInheritanceTest b = new TestCls2();
if(a is TestCls2)
{
throw new Exception("Error");
}
}
public static void InheritanceTest10()
{
List<Parent> list = new List<Parent>();
Child2 c1 = new Child2();
SubParent c2 = new Child3();
list.Add(new Child2());
list.Add(new Child3());
list.RemoveAll((c) => c is Child2);
if (list.Count != 1)
throw new Exception("Error");
var c3 = (Child3)c2;
Console.WriteLine(c3.ToString());
c1 = c2 as Child2;
Console.WriteLine(c1 == null);
}
public static void InheritanceTest11()
{
SubParent sub = new SubParent();
sub.Test();
}
static void InheritanceTest12Sub()
{
Console.WriteLine("OK");
}
public static void InheritanceTest12()
{
for(int i = 0; i < 5; i++)
{
System.Type t = typeof(InheritanceTest);
t.GetMethod("InheritanceTest12Sub", System.Reflection.BindingFlags.NonPublic | System.Reflection.BindingFlags.Static).Invoke(null, null);
}
}
public static void InheritanceTest13()
{
new TestClass().TestMethod();
}
public static void InheritanceTest14()
{
Data2 data = new Data2();
GenericInheritanceTestCls<Data2> instance = new GenericInheritanceTestCls<Data2>(data);
instance.TestVirtual();
}
public static void InheritanceTest15()
{
var cls = new TestCls7();
int val = 0;
cls.VMethod3(ref val);
if (val != 1)
throw new Exception();
}
public static void InheritanceTest16()
{
InheritanceTest16SubCls2 cls = new InheritanceTest16SubCls2();
cls.KKK();
}
public static void InheritanceTest17()
{
ClassB b = new ClassB();
CrossClass crossClass = new CrossClass();
if (!b.OutMethod(out crossClass.classA)) return;
}
public static void InheritanceTest18()
{
TestCls5 obj = new TestCls5();
TestClass2.Register(obj);
TestCls5 res;
Alloc<TestCls5>(out res);
res.bbbb = 5000;
if (res.bbbb != 5000)
throw new Exception();
}
public static void InheritanceTest19()
{
TestExplicitInterface a = new TestExplicitInterface();
((IDisposable)a).Dispose();
if (!a.Called)
throw new Exception();
}
public static void InheritanceTest20()
{
var mapa = new MapA();
var mapb = new MapB();
mapb.Add("11", new BModel());
var aList = new System.Collections.Generic.List<AModel>();
var bList = new System.Collections.Generic.List<BModel>();
mapa.GetAll(aList);
mapb.GetAll(bList);
if (bList.Count < 1)
{
throw new Exception();
}
}
CrossClass crossClass;
public static void InheritanceTest21()
{
var inheritanceTest = new InheritanceTest();
var dic = new Dictionary<int, CrossClass>();
dic.Add(1, new CrossClass());
dic.TryGetValue(1, out inheritanceTest.crossClass);
Console.WriteLine("InheritanceTest21:classA is " + inheritanceTest.crossClass.classA);
}
public static void InheritanceTest22()
{
var inheritanceTest = new InheritanceTest();
var dic = new Dictionary<int, CrossClass>();
dic.Add(1, new CrossClass());
dic.TryGetValue(1, out inheritanceTest.crossClass);
inheritanceTest.crossClass.classA = new ClassA();
Console.WriteLine("InheritanceTest21:classA is " + inheritanceTest.crossClass.classA);
}
[ILRuntimeTest.ILRuntimeTest(ExpectException = typeof(InvalidCastException))]
public static void InheritanceTest23()
{
TestClass cls = new TestClass();
if (cls is InterfaceTest2)
throw new Exception();
InterfaceTest2 cls2 = cls as InterfaceTest2;
if (cls2 is InterfaceTest2)
throw new Exception();
TestClass3 cls3 = (TestClass3)(cls as InterfaceTest2);
if(cls3 != null)
throw new Exception();
cls2 = new TestCls3();
var cls4 = (TestCls3)cls2;
if(cls4 == null)
throw new Exception();
InterfaceTest2 cls5 = (InterfaceTest2)cls;
if (cls5 is InterfaceTest2)
throw new Exception();
}
public static void InheritanceTest24()
{
ListSub list = new ListSub();
float res = list.Update(0, 1);
if (res < 10)
throw new Exception();
res = list.Update2(0, 1);
if (res < 10)
throw new Exception();
}
class ListSub : AbstractBase
{
List<AbstractBase> children = new List<AbstractBase>();
public ListSub()
{
for (int i = 0; i < 10; i++)
children.Add(new ListSub2());
}
public override float Update(float a, float b)
{
float baseVal = a;
foreach (var i in children)
{
baseVal += i.Update(baseVal, b);
}
return baseVal;
}
public override float Update2(float a, float b)
{
float baseVal = a;
foreach (var i in children)
{
baseVal += i.Update2(baseVal, b);
}
return baseVal;
}
}
class ListSub2 : AbstractBase
{
public override float Update(float a, float b)
{
return a + b;
}
[ILRuntimeJIT(ILRuntimeJITFlags.NoJIT)]
public override float Update2(float a, float b)
{
return a + b;
}
}
abstract class AbstractBase
{
[ILRuntimeJIT(ILRuntimeJITFlags.NoJIT)]
public abstract float Update(float a, float b);
public abstract float Update2(float a, float b);
}
class TestExplicitInterface : IDisposable
{
public bool Called { get; set; }
void IDisposable.Dispose()
{
Called = true;
}
}
static void Alloc<T>(out T value)where T : TestClass2
{
value = TestClass2.Alloc() as T;
}
class CrossClass : TestClass3
{
public ClassA classA;
}
class ClassA
{
}
class ClassC
{
public void Init()
{
ClassB b = new ClassB();
CrossClass crossClass = new CrossClass();
if (!b.OutMethod(out crossClass.classA)) return;
}
}
class ClassB
{
public bool OutMethod(out ClassA a)
{
a = new ClassA();
if (a == null)
{
throw new Exception("classA is null");
}
return true;
}
}
class InheritanceTest16SubCls : TestClass2
{
public int constVal = 11111;
protected override void AbMethod1()
{
}
public override float AbMethod2(int arg1)
{
return arg1 * 12333f;
}
public int Test(int arg)
{
return arg + constVal;
}
}
class InheritanceTest16SubCls2 : TestClass4
{
public override void KKK()
{
cls2 = new InheritanceTest16SubCls();
float res = cls2.AbMethod2(1);
if (Math.Abs(12333 - res) > 0.00001f)
throw new Exception();
int res2 = ((InheritanceTest16SubCls)cls2).Test(5);
if (res2 != 11116)
throw new Exception();
}
}
public interface IData { }
public class Data : IData { }
public abstract class Test<T> where T : class, IData
{
public T Data { get; protected set; }
public void TestMethod()
{
Data = null;//此处赋值为null会出现异常, 非null不会发生异常
Console.WriteLine(Data);
}
}
public class TestClass : Test<Data>
{
}
class TestCls5 : TestClass2
{
public int bbbb;
public override void VMethod3(ref int arg)
{
base.VMethod3(ref arg);
arg += 10;
}
public override bool VMethod2()
{
return base.VMethod2();
}
public override float AbMethod2(int arg1)
{
return arg1 + 1.2f;
}
protected override void AbMethod1()
{
}
public override string ToString()
{
return base.ToString();
}
}
class TestCls6 : TestClass2
{
public override void VMethod3(ref int arg)
{
base.VMethod3(ref arg);
arg += 20;
}
public override bool VMethod2()
{
return base.VMethod2();
}
public override float AbMethod2(int arg1)
{
return arg1 + 3.2f;
}
protected override void AbMethod1()
{
}
}
class TestCls7 : TestClass2
{
public override float AbMethod2(int arg1)
{
return arg1 + 1.2f;
}
protected override void AbMethod1()
{
}
}
class BaseData
{
public string m_Message;
}
class Data2: BaseData
{
public Data2()
{
m_Message = "hello";
}
}
class GenericInheritanceTestCls<T> : ClassInheritanceTest where T : BaseData
{
protected T m_Data; //泛型字段
public GenericInheritanceTestCls(T data)
{
m_Data = data;
}
public override void TestVirtual()
{
m_Data = null; //这行可能把stack写坏了
TestAbstract(); //运行到这行会报错,解决方法有2个:1、将"m_Data = null;"这行注释掉。2、将"protected T m_Data;" 改为 "protected BaseData m_Data;",去掉泛型字段
}
public override void TestAbstract()
{
Console.WriteLine("OK");
}
}
class GenericTestCls<T>
where T : ClassInheritanceTest, new()
{
T instance = new T();
public void DoTest()
{
instance.TestAbstract();
instance.TestVirtual();
instance.TestField();
}
}
public static void InheritanceTest06()
{
InheritanceTest it = new InheritanceTest();
it.InheritanceTest06_Sub<MyClass>();
}
void InheritanceTest06_Sub<T>() where T:MyClass
{
T obj = Activator.CreateInstance(typeof(T)) as T; //这样写错误
//MyClass obj = Activator.CreateInstance(typeof(T)) as MyClass; //这样写正确
obj.Reg();
}
interface IMy
{
}
class MyClass
{
public void Reg()
{
if (this is IMy)
{
Console.WriteLine("is IMy"); //正确结果不应该打印这句;但实际上会打印这句,还会抛一个异常
}
else
{
Console.WriteLine("not is IMy");
}
}
}
}
class TestCls : ClassInheritanceTest
{
public TestCls()
{
testVal = 1;
}
public override void TestAbstract()
{
Console.WriteLine("This is TestCls.TestAbstract");
}
public override void TestVirtual()
{
base.TestVirtual();
Console.WriteLine("This is TestCls.TestVirtual");
}
public override string ToString()
{
return "This is TestCls";
}
}
class TestCls2 : ClassInheritanceTest, IAs1
{
public TestCls2()
: base(4, 5)
{
testVal = 2;
}
public void AA1()
{
Console.WriteLine("This is TestCls2.AA1");
}
public override void TestAbstract()
{
Console.WriteLine("This is TestCls2.TestAbstract");
}
public override void TestVirtual()
{
Console.WriteLine("This is TestCls2.TestVirtual");
}
}
class TestCls3 : InterfaceTest2
{
int testVal;
public TestCls3()
{
testVal = 3;
}
public void TestAbstract()
{
Console.WriteLine("This is TestCls3.TestAbstract");
}
public void TestField()
{
Console.WriteLine("testValChild = " + testVal);
}
void InterfaceTest2.TestVirtual()
{
Console.WriteLine("TestVirtual = " + testVal);
}
}
class TestCls4 : ClassInheritanceTest2<TestCls4>
{
public override void TestVirtual()
{
base.TestVirtual();
}
}
interface IAs1
{
void AA1();
}
class AABase : IDisposable
{
public int R;
public int R2 { get; set; }
public void AA1()
{
Console.WriteLine("AABase");
}
public void Dispose()
{
}
}
class AA : AABase
{
public new void AA1()
{
Console.WriteLine("AA1");
}
}
class BB : ILRuntimeTest.TestFramework.ClassInheritanceTest, IAs1
{
public void AA1()
{
Console.WriteLine("AA1");
}
public override void TestAbstract()
{
Console.WriteLine("BB");
}
}
public class Parent
{
public virtual void Test()
{
Console.WriteLine("Parent.test");
}
}
public class SubParent: Parent
{
public override void Test()
{
base.Test();
Console.WriteLine("SubParent.test");
}
}
public class Child2 : SubParent
{
}
public class Child3 : SubParent
{
}
public interface IAwake
{
void Awake();
}
public class Child : Parent, IAwake
{
public void Awake()
{
}
}
class TestAs
{
public static bool TestAs01()
{
AA aa = new AA();
if (aa is IAs1)
{
throw new Exception("error");
}
return true;
}
public static bool TestAs02()
{
AA aa = new AA();
IAs1 ias = aa as IAs1;
if (ias != null)
{
throw new Exception("error");
}
return true;
}
public static bool TestAs03()
{
BB aa = new BB();
Dictionary<int, BB> dic = new Dictionary<int, BB>();
dic[0] = aa;
IAs1 ias = dic[0] as IAs1;
if (ias == null)
{
throw new Exception("error");
}
else
ias.AA1();
ClassInheritanceTest id = dic[0] as ClassInheritanceTest;
if (id == null)
{
throw new Exception("error2");
}
else
id.TestAbstract();
return true;
}
public static bool TestAs04()
{
object child = new Child();
IAwake awake = child as IAwake;
if (awake == null)
{
Console.WriteLine("as fail!");
throw new Exception("error");
}
return true;
}
}
class TestIs
{
public class Base
{
public virtual int Value()
{
return 0;
}
}
public class Base2 : Base
{
}
public class Base3 : Base2
{
}
public static bool TestIs01()
{
AA aa = new AA();
if (aa is IAs1)
{
throw new Exception("error");
}
return true;
}
public static bool TestIs02()
{
AA aa = new AA();
if (aa is AABase == false)
{
throw new Exception("error");
}
return true;
}
public static bool TestIs03()
{
AA aa = new AA();
if (aa is IDisposable == false)
{
throw new Exception("error");
}
return true;
}
public static bool TestIs04()
{
IDisposable aa = new AA();
if (aa is AABase == false)
{
throw new Exception("error");
}
return true;
}
public static bool TestIs05()
{
Base aa = new Base3();
if (aa is Base2 == false)
{
throw new Exception("error");
}
return true;
}
public static void TestInterface()
{
var obj = new AA();
ClassInheritanceTest.staticField = obj;
ClassInheritanceTest.staticField.Dispose();
}
}
public class TestVirtual
{
public class Base
{
public virtual int Value()
{
return 0;
}
}
public class Base2 : Base
{
public override int Value()
{
return 2;
}
}
public class Base3 : Base2
{
}
public static bool TestVirtualMethod01()
{
Base aa = new Base3();
if (aa.Value() == 2)
{
return true;
}
throw new Exception("error");
}
public static bool TestVirtualMethod02()
{
Base aa = new Base2();
if (aa.Value() == 2)
{
return true;
}
throw new Exception("error");
}
public static bool TestVirtualMethod03()
{
Base2 aa = new Base2();
if (aa.Value() == 2)
{
return true;
}
throw new Exception("error");
}
}
public interface IModel { }
public class AModel : IModel { }
public class BModel : IModel { }