|
| 1 | +// Licensed to the .NET Foundation under one or more agreements. |
| 2 | +// The .NET Foundation licenses this file to you under the MIT license. |
| 3 | +// See the LICENSE file in the project root for more information. |
| 4 | + |
| 5 | +using System; |
| 6 | +using System.Reflection; |
| 7 | + |
| 8 | +class Program |
| 9 | +{ |
| 10 | + static int Main() |
| 11 | + { |
| 12 | + var baseClass = new BaseClass(); |
| 13 | + var derivedClass = new DerivedClass(); |
| 14 | + var superDerivedClass = new SuperDerivedClass(); |
| 15 | + |
| 16 | + // |
| 17 | + // C# will generate a call to the unmodified version of the method so this is easy to check statically |
| 18 | + // |
| 19 | + |
| 20 | + if (baseClass.Override(0) != 3) |
| 21 | + return 1; |
| 22 | + |
| 23 | + if (derivedClass.Override(0) != 103) |
| 24 | + return 2; |
| 25 | + |
| 26 | + if (superDerivedClass.Override(0) != 203) |
| 27 | + return 3; |
| 28 | + |
| 29 | + // |
| 30 | + // Reflection-locate rest of the `Override` method overloads |
| 31 | + // |
| 32 | + |
| 33 | + MethodInfo paramModoptFoo = null; |
| 34 | + MethodInfo paramModoptBar = null; |
| 35 | + MethodInfo paramModreqFoo = null; |
| 36 | + MethodInfo paramUnmodified = null; |
| 37 | + MethodInfo returnModoptFoo = null; |
| 38 | + MethodInfo arrayModopt1 = null; |
| 39 | + MethodInfo arrayModopt2 = null; |
| 40 | + foreach (var method in typeof(BaseClass).GetTypeInfo().DeclaredMethods) |
| 41 | + { |
| 42 | + ParameterInfo param = method.GetParameters()[0]; |
| 43 | + Type[] paramRequiredModifiers = param.GetRequiredCustomModifiers(); |
| 44 | + Type[] paramOptionalModifiers = param.GetOptionalCustomModifiers(); |
| 45 | + |
| 46 | + ParameterInfo retParam = method.ReturnParameter; |
| 47 | + Type[] retParamOptionalModifiers = retParam.GetOptionalCustomModifiers(); |
| 48 | + |
| 49 | + if (param.ParameterType != typeof(int) && param.ParameterType != typeof(int[])) |
| 50 | + throw new Exception(); |
| 51 | + |
| 52 | + if (paramRequiredModifiers.Length > 0) |
| 53 | + { |
| 54 | + if (paramRequiredModifiers.Length > 1 || paramRequiredModifiers[0] != typeof(FooModifier)) |
| 55 | + throw new Exception(); |
| 56 | + else |
| 57 | + paramModreqFoo = method; |
| 58 | + } |
| 59 | + else if (paramOptionalModifiers.Length > 0) |
| 60 | + { |
| 61 | + if (paramOptionalModifiers.Length > 1) |
| 62 | + throw new Exception(); |
| 63 | + else if (paramOptionalModifiers[0] == typeof(FooModifier)) |
| 64 | + paramModoptFoo = method; |
| 65 | + else if (paramOptionalModifiers[0] == typeof(BarModifier)) |
| 66 | + paramModoptBar = method; |
| 67 | + else |
| 68 | + throw new Exception(); |
| 69 | + } |
| 70 | + else if (retParamOptionalModifiers.Length > 0) |
| 71 | + { |
| 72 | + if (retParamOptionalModifiers.Length > 1 || retParamOptionalModifiers[0] != typeof(FooModifier)) |
| 73 | + throw new Exception(); |
| 74 | + else |
| 75 | + returnModoptFoo = method; |
| 76 | + } |
| 77 | + else |
| 78 | + { |
| 79 | + if (param.ParameterType == typeof(int)) |
| 80 | + paramUnmodified = method; |
| 81 | + else if (param.ParameterType == typeof(int[])) |
| 82 | + { |
| 83 | + // Reflection can't distinguish between the two overloads |
| 84 | + |
| 85 | + if (arrayModopt1 == null) |
| 86 | + arrayModopt1 = method; |
| 87 | + else if (arrayModopt2 == null) |
| 88 | + arrayModopt2 = method; |
| 89 | + else |
| 90 | + throw new Exception(); |
| 91 | + } |
| 92 | + else |
| 93 | + throw new Exception(); |
| 94 | + } |
| 95 | + } |
| 96 | + |
| 97 | + if ((int)paramModoptFoo.Invoke(baseClass, new object[] { 0 }) != 0) |
| 98 | + return 101; |
| 99 | + if ((int)paramModoptBar.Invoke(baseClass, new object[] { 0 }) != 1) |
| 100 | + return 102; |
| 101 | + if ((int)paramModreqFoo.Invoke(baseClass, new object[] { 0 }) != 2) |
| 102 | + return 103; |
| 103 | + if ((int)paramUnmodified.Invoke(baseClass, new object[] { 0 }) != 3) |
| 104 | + return 104; |
| 105 | + if ((int)returnModoptFoo.Invoke(baseClass, new object[] { 0 }) != 4) |
| 106 | + return 105; |
| 107 | + |
| 108 | + if ((int)paramModoptFoo.Invoke(derivedClass, new object[] { 0 }) != 100) |
| 109 | + return 201; |
| 110 | + if ((int)paramModoptBar.Invoke(derivedClass, new object[] { 0 }) != 101) |
| 111 | + return 202; |
| 112 | + if ((int)paramModreqFoo.Invoke(derivedClass, new object[] { 0 }) != 102) |
| 113 | + return 203; |
| 114 | + if ((int)paramUnmodified.Invoke(derivedClass, new object[] { 0 }) != 103) |
| 115 | + return 204; |
| 116 | + if ((int)returnModoptFoo.Invoke(derivedClass, new object[] { 0 }) != 104) |
| 117 | + return 205; |
| 118 | + |
| 119 | + if ((int)arrayModopt1.Invoke(baseClass, new object[] { null }) + 100 != (int)arrayModopt1.Invoke(derivedClass, new object[] { null })) |
| 120 | + return 301; |
| 121 | + |
| 122 | + if ((int)arrayModopt2.Invoke(baseClass, new object[] { null }) + 100 != (int)arrayModopt2.Invoke(derivedClass, new object[] { null })) |
| 123 | + return 302; |
| 124 | + |
| 125 | + // |
| 126 | + // Make sure modifiers are ignored for newobj/box |
| 127 | + // |
| 128 | + |
| 129 | + object tryAllocWithModifiedArrayResult = Factory.TryAllocWithModifiedArray(); |
| 130 | + if (!(tryAllocWithModifiedArrayResult is GenericClass<int[]>)) |
| 131 | + return 401; |
| 132 | + if (tryAllocWithModifiedArrayResult.GetType() != typeof(GenericClass<int[]>)) |
| 133 | + return 402; |
| 134 | + |
| 135 | + object tryBoxWithModifiedPointerResult = Factory.TryBoxWithModifiedPointer(); |
| 136 | + if (!(tryBoxWithModifiedPointerResult is GenericStruct<int*[]>)) |
| 137 | + return 501; |
| 138 | + if (tryBoxWithModifiedPointerResult.GetType() != typeof(GenericStruct<int*[]>)) |
| 139 | + return 502; |
| 140 | + |
| 141 | + return 100; |
| 142 | + } |
| 143 | + |
| 144 | + class SuperDerivedClass : DerivedClass |
| 145 | + { |
| 146 | + public override int Override(int A_0) |
| 147 | + { |
| 148 | + Console.WriteLine("In int32 SuperDerivedClass::Override(int32)"); |
| 149 | + return 203; |
| 150 | + } |
| 151 | + } |
| 152 | +} |
0 commit comments