Welcome to mirror list, hosted at ThFree Co, Russian Federation.

ConstructorUsedViaReflection.cs « Reflection « Mono.Linker.Tests.Cases « test - github.com/mono/linker.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: dde8f46f977adaed54c889b7001ea2d3e23eb3b9 (plain)
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
using System;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Metadata;

namespace Mono.Linker.Tests.Cases.Reflection
{
	[SetupCSharpCompilerToUse ("csc")]
	public class ConstructorUsedViaReflection
	{
		public static void Main ()
		{
			GetConstructor_Types.Test ();
			GetConstructor_BindingAttr_Binder_Types_Modifiers.TestWithBindingFlags ();
			GetConstructor_BindingAttr_Binder_Types_Modifiers.TestWithUnknownBindingFlags (BindingFlags.Public);
			GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers.TestWithCallingConvention ();
#if NETCOREAPP
			GetConstructor_BindingAttr_Types.Test ();
#endif
			TestNullType ();
			TestDataFlowType ();
			IfElse.TestIfElse (true);
		}

		[Kept]
		private class GetConstructor_Types
		{
			[Kept]
			public GetConstructor_Types ()
			{ }

			[Kept]
			public GetConstructor_Types (int i)
			{ }

			private GetConstructor_Types (string foo)
			{ }

			protected GetConstructor_Types (string foo, string bar)
			{ }

			[Kept]
			class EmptyTypes
			{
				[Kept]
				public EmptyTypes () { }
				public EmptyTypes (int i) { }
			}

			[Kept]
			public static void Test ()
			{
				TestConstructorWithTypes_EmptyTypes ();
				TestConstructorWithTypes_NonEmptyTypes ();
				TestConstructorWithTypes_EmptyTypes_DataFlow (typeof (TestType));
				TestConstructorWithTypes_NonEmptyTypes_DataFlow (typeof (TestType));
			}

			[Kept]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (Type[]) },
				typeof (EmptyTypes), nameof (EmptyTypes), new Type[0])]
			static void TestConstructorWithTypes_EmptyTypes ()
			{
				var constructor = typeof (EmptyTypes).GetConstructor (new Type[] { });
				constructor.Invoke (null, new object[] { });
			}

			[Kept]
			[RecognizedReflectionAccessPattern]
			static void TestConstructorWithTypes_NonEmptyTypes ()
			{
				var constructor = typeof (GetConstructor_Types).GetConstructor (new Type[] { typeof (int) });
				constructor.Invoke (null, new object[] { null });
			}

			[Kept]
			[RecognizedReflectionAccessPattern]
			static void TestConstructorWithTypes_EmptyTypes_DataFlow (
				[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
				[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
				Type type)
			{
				var constructor = type.GetConstructor (new Type[] { });
				constructor.Invoke (null, new object[] { });
			}

			[Kept]
			[UnrecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (Type[]) }, messageCode: "IL2070")]
			static void TestConstructorWithTypes_NonEmptyTypes_DataFlow (
				[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
				[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
				Type type)
			{
				var constructor = type.GetConstructor (new Type[] { typeof (int) });
				constructor.Invoke (null, new object[] { null });
			}
		}

		[Kept]
		class GetConstructor_BindingAttr_Binder_Types_Modifiers
		{
			[Kept]
			class KnownBindingFlags
			{
				[Kept]
				public KnownBindingFlags ()
				{ }

				public KnownBindingFlags (string bar)
				{ }

				private KnownBindingFlags (int foo)
				{ }

				protected KnownBindingFlags (int foo, int bar)
				{ }

				internal KnownBindingFlags (int foo, int bar, int baz)
				{ }
			}

			[Kept]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Binder), typeof (Type[]), typeof (ParameterModifier[]) },
				typeof (KnownBindingFlags), nameof (KnownBindingFlags), new Type[0])]
			public static void TestWithBindingFlags ()
			{
				var constructor = typeof (KnownBindingFlags).GetConstructor (BindingFlags.Public, GetNullValue ("some argument", 2, 3), new Type[] { }, new ParameterModifier[] { });
				constructor.Invoke (null, new object[] { });
			}

			[Kept]
			class UnknownBindingFlags
			{
				[Kept]
				public UnknownBindingFlags ()
				{ }

				[Kept]
				public UnknownBindingFlags (string bar)
				{ }

				[Kept]
				private UnknownBindingFlags (int foo)
				{ }

				[Kept]
				protected UnknownBindingFlags (int foo, int bar)
				{ }

				[Kept]
				internal UnknownBindingFlags (int foo, int bar, int baz)
				{ }
			}

			[Kept]
			[RecognizedReflectionAccessPattern (typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Binder), typeof (Type[]), typeof (ParameterModifier[]) },
					typeof (UnknownBindingFlags), nameof (UnknownBindingFlags), new Type[0])]
			public static void TestWithUnknownBindingFlags (BindingFlags bindingFlags)
			{
				// Since the binding flags are not known linker should mark all constructors on the type
				var constructor = typeof (UnknownBindingFlags).GetConstructor (bindingFlags, GetNullValue ("some argument", 2, 3), new Type[] { }, new ParameterModifier[] { });
				constructor.Invoke (null, new object[] { });
			}
		}

		[Kept]
		class GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers
		{
			[Kept]
			public GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers ()
			{ }

			public GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers (string bar)
			{ }

			private GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers (int foo)
			{ }

			protected GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers (int foo, int bar)
			{ }

			internal GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers (int foo, int bar, int baz)
			{ }

			[Kept]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Binder), typeof (CallingConventions), typeof (Type[]), typeof (ParameterModifier[]) },
				typeof (GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers), nameof (GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers), new Type[0])]
			public static void TestWithCallingConvention ()
			{
				var constructor = typeof (GetConstructor_BindingAttr_Binder_CallConvention_Types_Modifiers).GetConstructor (BindingFlags.Public, GetNullValue ("some argument", 2, 3), CallingConventions.HasThis, new Type[] { }, new ParameterModifier[] { });
				constructor.Invoke (null, new object[] { });
			}
		}

#if NETCOREAPP
		[Kept]
		class GetConstructor_BindingAttr_Types
		{
			[Kept]
			public GetConstructor_BindingAttr_Types ()
			{ }

			public GetConstructor_BindingAttr_Types (string bar)
			{ }

			private GetConstructor_BindingAttr_Types (int foo)
			{ }

			protected GetConstructor_BindingAttr_Types (int foo, int bar)
			{ }

			internal GetConstructor_BindingAttr_Types (int foo, int bar, int baz)
			{ }

			[Kept]
			class NonEmptyTypes
			{
				[Kept]
				public NonEmptyTypes () { }
				[Kept]
				public NonEmptyTypes (int i) { }
			}

			[Kept]
			public static void Test ()
			{
				TestWithBindingFlagsAndTypes_EmptyTypes ();
				TestWithBindingFlagsAndTypes_NonEmptyTypes ();
				TestWithBindingFlagsAndTypes_EmptyTypes_DataFlow (null);
				TestWithBindingFlagsAndTypes_NonEmptyTypes_DataFlow (null);
			}

			[Kept]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Type[]) },
				typeof (GetConstructor_BindingAttr_Types), nameof (GetConstructor_BindingAttr_Types), new Type[0])]
			static void TestWithBindingFlagsAndTypes_EmptyTypes ()
			{
				var constructor = typeof (GetConstructor_BindingAttr_Types).GetConstructor (BindingFlags.Public, new Type[] { });
				constructor.Invoke (null, new object[] { });
			}

			[Kept]
			[RecognizedReflectionAccessPattern]
			static void TestWithBindingFlagsAndTypes_NonEmptyTypes ()
			{
				var constructor = typeof (NonEmptyTypes).GetConstructor (BindingFlags.Public, new Type[] { typeof (TestType) });
				constructor.Invoke (null, new object[] { null });
			}

			[Kept]
			[RecognizedReflectionAccessPattern]
			static void TestWithBindingFlagsAndTypes_EmptyTypes_DataFlow (
				[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
				[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
				Type type)
			{
				var constructor = type.GetConstructor (BindingFlags.Public, new Type[] { });
			}

			[Kept]
			[UnrecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Type[]) }, messageCode: "IL2070")]
			static void TestWithBindingFlagsAndTypes_NonEmptyTypes_DataFlow (
				[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
				[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
				Type type)
			{
				var constructor = type.GetConstructor (BindingFlags.Public, new Type[] { typeof (TestType) });
			}
		}
#endif

		[Kept]
		static void TestNullType ()
		{
			Type type = null;
			var constructor = type.GetConstructor (new Type[] { });
		}

		[Kept]
		static Type FindType ()
		{
			return null;
		}

		[UnrecognizedReflectionAccessPattern (typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (Type[]) },
			messageCode: "IL2075", message: new string[] { "FindType", "GetConstructor" })]
		[Kept]
		static void TestDataFlowType ()
		{
			Type type = FindType ();
			var constructor = type.GetConstructor (new Type[] { });
		}

		[Kept]
		class IfElse
		{
			[Kept]
			private class IfConstructor
			{
				[Kept]
				public IfConstructor ()
				{ }

				public IfConstructor (int foo)
				{ }

				private IfConstructor (string foo)
				{ }

				protected IfConstructor (int foo, int bar)
				{ }

				internal IfConstructor (int foo, string bar)
				{ }
			}

			[Kept]
			private class ElseConstructor
			{
				[Kept]
				public ElseConstructor ()
				{ }

				public ElseConstructor (int foo)
				{ }

				private ElseConstructor (string foo)
				{ }

				protected ElseConstructor (int foo, int bar)
				{ }

				internal ElseConstructor (int foo, string bar)
				{ }
			}

			[Kept]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Binder), typeof (Type[]), typeof (ParameterModifier[]) },
				typeof (IfConstructor), nameof (IfConstructor), new Type[0])]
			[RecognizedReflectionAccessPattern (
				typeof (Type), nameof (Type.GetConstructor), new Type[] { typeof (BindingFlags), typeof (Binder), typeof (Type[]), typeof (ParameterModifier[]) },
				typeof (ElseConstructor), nameof (ElseConstructor), new Type[0])]
			public static void TestIfElse (bool decision)
			{
				Type myType;
				if (decision) {
					myType = typeof (IfConstructor);
				} else {
					myType = typeof (ElseConstructor);
				}
				var constructor = myType.GetConstructor (BindingFlags.Public, null, new Type[] { }, null);
				constructor.Invoke (null, new object[] { });
			}
		}

		[Kept]
		static Binder GetNullValue (string str, int i, long g)
		{
			return null;
		}

		[Kept]
		[KeptMember (".ctor()")]
		class TestType
		{
		}
	}
}