Debian Codegraph @

external/corefx/src/System.Reflection.Emit/tests/MethodBuilder/MethodBuilderSetSignature.cs

raw
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 Xunit;
6
7 namespace System.Reflection.Emit.Tests
8 {
9 public class MethodBuilderSetSignature
10 {
11 [Fact]
12 public void SetSignature_GenericMethod_SingleGenericParameter()
13 {
14 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
15 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
16
17 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
18 GenericTypeParameterBuilder returnType = typeParameters[0];
19
20 method.SetSignature(returnType.AsType(), null, null, null, null, null);
21 VerifyMethodSignature(type, method, returnType.AsType());
22 }
23
24 [Fact]
25 public void SetSignature_GenericMethod_MultipleGenericParameters()
26 {
27 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
28 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
29
30 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T", "U");
31 GenericTypeParameterBuilder desiredReturnType = typeParameters[1];
32
33 method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null);
34 VerifyMethodSignature(type, method, desiredReturnType.AsType());
35 }
36
37 [Fact]
38 public void SetSignature_GenericMethod_ReturnType_RequiredCustomModifiers()
39 {
40 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
41 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
42
43 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
44 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
45
46 method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null);
47 VerifyMethodSignature(type, method, desiredReturnType.AsType());
48 }
49
50 [Fact]
51 public void SetSignature_GenericMethod_ReturnType_RequiredModifier_OptionalCustomModifiers()
52 {
53 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
54 MethodBuilder method = type.DefineMethod("TestMethod",
55 MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
56
57 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
58 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
59
60 method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null);
61 VerifyMethodSignature(type, method, desiredReturnType.AsType());
62 }
63
64 [Fact]
65 public void SetSignature_GenericMethod_ReturnType_OptionalCustomModifiers()
66 {
67 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
68 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
69
70 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
71 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
72
73 method.SetSignature(desiredReturnType.AsType(), null, null, null, null, null);
74 VerifyMethodSignature(type, method, desiredReturnType.AsType());
75 }
76
77 [Fact]
78 public void SetSignature_GenericMethod_ParameterTypes()
79 {
80 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
81 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
82
83 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
84 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
85 Type[] desiredParamType = new Type[] { typeof(int) };
86
87 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, null);
88 VerifyMethodSignature(type, method, desiredReturnType.AsType());
89 }
90
91 [Fact]
92 public void SetSignature_GenericMethod_MultipleParameters()
93 {
94 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
95 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
96
97 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T", "U");
98 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
99 Type[] desiredParamType = new Type[] { typeof(int), typeParameters[1].AsType() };
100
101 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, null);
102 VerifyMethodSignature(type, method, desiredReturnType.AsType());
103 }
104
105 [Fact]
106 public void SetSignature_GenericMethod_ParameterType_RequiredCustomModifiers()
107 {
108 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
109 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
110
111 string[] typeParamNames = new string[] { "T" };
112 GenericTypeParameterBuilder[] typeParameters =
113 method.DefineGenericParameters(typeParamNames);
114 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
115 Type[] desiredParamType = new Type[] { typeof(int) };
116 Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][];
117 for (int i = 0; i < desiredParamType.Length; ++i)
118 {
119 parameterTypeRequiredCustomModifiers[i] = null;
120 }
121
122 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, null);
123
124 VerifyMethodSignature(type, method, desiredReturnType.AsType());
125 }
126
127 [Fact]
128 public void SetSignature_GenericMethod_ParameterType_RequiredCustomModifer_OptionalCustomModifiers()
129 {
130 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
131 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
132
133 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
134 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
135 Type[] desiredParamType = new Type[] { typeof(int) };
136 Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][];
137 Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][];
138 for (int i = 0; i < desiredParamType.Length; ++i)
139 {
140 parameterTypeRequiredCustomModifiers[i] = null;
141 parameterTypeOptionalCustomModifiers[i] = null;
142 }
143
144 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
145 VerifyMethodSignature(type, method, desiredReturnType.AsType());
146 }
147
148 [Fact]
149 public void SetSignature_GenericMethod_ParameterType_OptionalCustomModifiers()
150 {
151 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
152 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
153
154 GenericTypeParameterBuilder[] typeParameters =
155 method.DefineGenericParameters("T");
156 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
157 Type[] desiredParamType = new Type[] { typeof(int) };
158 Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][];
159 for (int i = 0; i < desiredParamType.Length; ++i)
160 {
161 parameterTypeOptionalCustomModifiers[i] = null;
162 }
163
164 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, null, parameterTypeOptionalCustomModifiers);
165 VerifyMethodSignature(type, method, desiredReturnType.AsType());
166 }
167
168 [Fact]
169 public void SetSignature_NonGenericMethod()
170 {
171 Type[] parameterTypes = new Type[] { typeof(string), typeof(object) };
172 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
173 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual, typeof(void), parameterTypes);
174 string[] parameterNames = new string[parameterTypes.Length];
175 for (int i = 0; i < parameterNames.Length; ++i)
176 {
177 parameterNames[i] = "P" + i.ToString();
178 method.DefineParameter(i + 1, ParameterAttributes.In, parameterNames[i]);
179 }
180
181 Type desiredReturnType = typeof(void);
182 Type[] desiredParamType = new Type[] { typeof(int) };
183 Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][];
184 Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][];
185 for (int i = 0; i < desiredParamType.Length; ++i)
186 {
187 parameterTypeRequiredCustomModifiers[i] = null;
188 parameterTypeOptionalCustomModifiers[i] = null;
189 }
190
191 method.SetSignature(desiredReturnType, null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
192 }
193
194 [Fact]
195 public void SetSignature_AllParametersNull()
196 {
197 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
198 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
199
200 method.SetSignature(null, null, null, null, null, null);
201 VerifyMethodSignature(type, method, null);
202 }
203
204 [Fact]
205 public void SetSignature_NullReturnType_CustomModifiersSetToWrongTypes()
206 {
207 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
208 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
209
210 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
211 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
212
213 method.SetSignature(null, null, null, null, null, null);
214 VerifyMethodSignature(type, method, null);
215 }
216
217 [Fact]
218 public void SetSignature_NullOnReturnType_CustomModifiersSetCorrectly()
219 {
220 int arraySize = 10;
221 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
222 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
223
224 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
225 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
226 Type[] desiredParamType = null;
227 Type[][] parameterTypeRequiredCustomModifiers = new Type[arraySize][];
228 Type[][] parameterTypeOptionalCustomModifiers = new Type[arraySize][];
229 for (int i = 0; i < arraySize; ++i)
230 {
231 parameterTypeRequiredCustomModifiers[i] = null;
232 parameterTypeOptionalCustomModifiers[i] = null;
233 }
234
235 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
236 }
237
238 [Fact]
239 public void SetSignature_NullReturnType_RequiredCustomModifiers_OptionalCustomModifiers()
240 {
241 TypeBuilder type = Helpers.DynamicType(TypeAttributes.Abstract);
242 MethodBuilder method = type.DefineMethod("TestMethod", MethodAttributes.Public | MethodAttributes.Abstract | MethodAttributes.Virtual);
243
244 GenericTypeParameterBuilder[] typeParameters = method.DefineGenericParameters("T");
245 GenericTypeParameterBuilder desiredReturnType = typeParameters[0];
246 Type[] desiredParamType = new Type[] { typeof(void) };
247 Type[][] parameterTypeRequiredCustomModifiers = new Type[desiredParamType.Length][];
248 Type[][] parameterTypeOptionalCustomModifiers = new Type[desiredParamType.Length][];
249 for (int i = 0; i < desiredParamType.Length; ++i)
250 {
251 parameterTypeRequiredCustomModifiers[i] = null;
252 parameterTypeOptionalCustomModifiers[i] = null;
253 }
254
255 method.SetSignature(desiredReturnType.AsType(), null, null, desiredParamType, parameterTypeRequiredCustomModifiers, parameterTypeOptionalCustomModifiers);
256 }
257
258 private void VerifyMethodSignature(TypeBuilder type, MethodBuilder method, Type desiredReturnType)
259 {
260 Type ret = type.CreateTypeInfo().AsType();
261 MethodInfo methodInfo = method.GetBaseDefinition();
262 Type actualReturnType = methodInfo.ReturnType;
263
264 if (desiredReturnType == null)
265 {
266 Assert.Null(actualReturnType);
267 }
268 else
269 {
270 Assert.NotNull(actualReturnType);
271 Assert.Equal(desiredReturnType.Name, actualReturnType.Name);
272 Assert.True(actualReturnType.Equals(desiredReturnType));
273 }
274 }
275 }
276 }