forked from dotnet/runtime
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathmodifiers.cs
More file actions
153 lines (132 loc) · 5.64 KB
/
modifiers.cs
File metadata and controls
153 lines (132 loc) · 5.64 KB
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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Reflection;
using Xunit;
public class Program
{
[Fact]
public static unsafe int TestEntryPoint()
{
var baseClass = new BaseClass();
var derivedClass = new DerivedClass();
var superDerivedClass = new SuperDerivedClass();
//
// C# will generate a call to the unmodified version of the method so this is easy to check statically
//
if (baseClass.Override(0) != 3)
return 1;
if (derivedClass.Override(0) != 103)
return 2;
if (superDerivedClass.Override(0) != 203)
return 3;
//
// Reflection-locate rest of the `Override` method overloads
//
MethodInfo paramModoptFoo = null;
MethodInfo paramModoptBar = null;
MethodInfo paramModreqFoo = null;
MethodInfo paramUnmodified = null;
MethodInfo returnModoptFoo = null;
MethodInfo arrayModopt1 = null;
MethodInfo arrayModopt2 = null;
foreach (var method in typeof(BaseClass).GetTypeInfo().DeclaredMethods)
{
ParameterInfo param = method.GetParameters()[0];
Type[] paramRequiredModifiers = param.GetRequiredCustomModifiers();
Type[] paramOptionalModifiers = param.GetOptionalCustomModifiers();
ParameterInfo retParam = method.ReturnParameter;
Type[] retParamOptionalModifiers = retParam.GetOptionalCustomModifiers();
if (param.ParameterType != typeof(int) && param.ParameterType != typeof(int[]))
throw new Exception();
if (paramRequiredModifiers.Length > 0)
{
if (paramRequiredModifiers.Length > 1 || paramRequiredModifiers[0] != typeof(FooModifier))
throw new Exception();
else
paramModreqFoo = method;
}
else if (paramOptionalModifiers.Length > 0)
{
if (paramOptionalModifiers.Length > 1)
throw new Exception();
else if (paramOptionalModifiers[0] == typeof(FooModifier))
paramModoptFoo = method;
else if (paramOptionalModifiers[0] == typeof(BarModifier))
paramModoptBar = method;
else
throw new Exception();
}
else if (retParamOptionalModifiers.Length > 0)
{
if (retParamOptionalModifiers.Length > 1 || retParamOptionalModifiers[0] != typeof(FooModifier))
throw new Exception();
else
returnModoptFoo = method;
}
else
{
if (param.ParameterType == typeof(int))
paramUnmodified = method;
else if (param.ParameterType == typeof(int[]))
{
// Reflection can't distinguish between the two overloads
if (arrayModopt1 == null)
arrayModopt1 = method;
else if (arrayModopt2 == null)
arrayModopt2 = method;
else
throw new Exception();
}
else
throw new Exception();
}
}
if ((int)paramModoptFoo.Invoke(baseClass, new object[] { 0 }) != 0)
return 101;
if ((int)paramModoptBar.Invoke(baseClass, new object[] { 0 }) != 1)
return 102;
if ((int)paramModreqFoo.Invoke(baseClass, new object[] { 0 }) != 2)
return 103;
if ((int)paramUnmodified.Invoke(baseClass, new object[] { 0 }) != 3)
return 104;
if ((int)returnModoptFoo.Invoke(baseClass, new object[] { 0 }) != 4)
return 105;
if ((int)paramModoptFoo.Invoke(derivedClass, new object[] { 0 }) != 100)
return 201;
if ((int)paramModoptBar.Invoke(derivedClass, new object[] { 0 }) != 101)
return 202;
if ((int)paramModreqFoo.Invoke(derivedClass, new object[] { 0 }) != 102)
return 203;
if ((int)paramUnmodified.Invoke(derivedClass, new object[] { 0 }) != 103)
return 204;
if ((int)returnModoptFoo.Invoke(derivedClass, new object[] { 0 }) != 104)
return 205;
if ((int)arrayModopt1.Invoke(baseClass, new object[] { null }) + 100 != (int)arrayModopt1.Invoke(derivedClass, new object[] { null }))
return 301;
if ((int)arrayModopt2.Invoke(baseClass, new object[] { null }) + 100 != (int)arrayModopt2.Invoke(derivedClass, new object[] { null }))
return 302;
//
// Make sure modifiers are ignored for newobj/box
//
object tryAllocWithModifiedArrayResult = Factory.TryAllocWithModifiedArray();
if (!(tryAllocWithModifiedArrayResult is GenericClass<int[]>))
return 401;
if (tryAllocWithModifiedArrayResult.GetType() != typeof(GenericClass<int[]>))
return 402;
object tryBoxWithModifiedPointerResult = Factory.TryBoxWithModifiedPointer();
if (!(tryBoxWithModifiedPointerResult is GenericStruct<int*[]>))
return 501;
if (tryBoxWithModifiedPointerResult.GetType() != typeof(GenericStruct<int*[]>))
return 502;
return 100;
}
class SuperDerivedClass : DerivedClass
{
public override int Override(int A_0)
{
Console.WriteLine("In int32 SuperDerivedClass::Override(int32)");
return 203;
}
}
}