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
|
using System;
using System.Diagnostics.CodeAnalysis;
using System.Globalization;
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")]
[KeptMember (".ctor()")]
public class ActivatorCreateInstance
{
public static void Main ()
{
Activator.CreateInstance (typeof (Test1));
Activator.CreateInstance (typeof (Test2), true);
Activator.CreateInstance (typeof (Test3), BindingFlags.NonPublic | BindingFlags.Instance, null, null, null);
Activator.CreateInstance (typeof (Test4), GetBindingFlags (), null, GetArgs (), null);
Activator.CreateInstance (typeof (Test5), new object[] { 1, "ss" });
var p = new ActivatorCreateInstance ();
// Direct call to static method with a parameter
FromParameterOnStaticMethod (typeof (FromParameterOnStaticMethodType));
// Value comes from multiple sources - each must be marked appropriately
Type fromParameterOnStaticMethodType = p == null ?
typeof (FromParameterOnStaticMethodTypeA) :
typeof (FromParameterOnStaticMethodTypeB);
FromParameterOnStaticMethod (fromParameterOnStaticMethodType);
// Direct call to instance method with a parameter
p.FromParameterOnInstanceMethod (typeof (FromParameterOnInstanceMethodType));
// Non-public constructors required
FromParameterWithNonPublicConstructors (typeof (FromParameterWithNonPublicConstructorsType));
// Public contructors required
FromParameterWithPublicConstructors (typeof (FromParameterWithPublicConstructorsType));
WithAssemblyName ();
WithAssemblyPath ();
AppDomainCreateInstance ();
UnsupportedCreateInstance ();
TestCreateInstanceOfTWithConcreteType ();
TestCreateInstanceOfTWithNewConstraint<TestCreateInstanceOfTWithNewConstraintType> ();
TestCreateInstanceOfTWithNoConstraint<TestCreateInstanceOfTWithNoConstraintType> ();
TestCreateInstanceOfTWithDataflow<TestCreateInstanceOfTWithDataflowType> ();
TestNullArgsOnKnownType ();
TestNullArgsOnAnnotatedType (typeof (TestType));
TestNullArgsNonPublicOnly (typeof (TestType));
TestNullArgsNonPublicWithNonPublicAnnotation (typeof (TestType));
CreateInstanceWithGetTypeFromHierarchy.Test ();
}
[Kept]
class Test1
{
[Kept]
public Test1 ()
{
}
public Test1 (int arg)
{
}
}
[Kept]
class Test2
{
[Kept]
private Test2 ()
{
}
public Test2 (int arg)
{
}
}
[Kept]
class Test3
{
[Kept]
private Test3 ()
{
}
public Test3 (int arg)
{
}
}
[Kept]
class Test4
{
[Kept]
public Test4 (int i, object o)
{
}
[Kept]
private Test4 (char b)
{
}
[Kept]
internal Test4 (int arg)
{
}
[Kept]
static Test4 ()
{
}
}
[Kept]
class Test5
{
[Kept]
public Test5 (int i, object o)
{
}
}
[Kept]
private static void FromParameterOnStaticMethod (
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
Type type)
{
Activator.CreateInstance (type);
}
[Kept]
class FromParameterOnStaticMethodType
{
[Kept]
public FromParameterOnStaticMethodType () { }
public FromParameterOnStaticMethodType (int arg) { }
}
[Kept]
class FromParameterOnStaticMethodTypeA
{
[Kept]
public FromParameterOnStaticMethodTypeA () { }
public FromParameterOnStaticMethodTypeA (int arg) { }
}
[Kept]
class FromParameterOnStaticMethodTypeB
{
[Kept]
public FromParameterOnStaticMethodTypeB () { }
public FromParameterOnStaticMethodTypeB (int arg) { }
}
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (Type), typeof (object[]) }, messageCode: "IL2067")]
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (Type), typeof (BindingFlags), typeof (Binder), typeof (object[]), typeof (CultureInfo) }, messageCode: "IL2067")]
[Kept]
private void FromParameterOnInstanceMethod (
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
Type type)
{
// Only default ctor is available from the parameter, so only the first one should work
Activator.CreateInstance (type);
Activator.CreateInstance (type, new object[] { 1 });
Activator.CreateInstance (type, BindingFlags.NonPublic, null, new object[] { 1, 2 }, null);
}
[Kept]
class FromParameterOnInstanceMethodType
{
[Kept]
public FromParameterOnInstanceMethodType () { }
public FromParameterOnInstanceMethodType (int arg) { }
public FromParameterOnInstanceMethodType (int arg, int arg2) { }
}
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (Type) }, messageCode: "IL2067")]
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (Type), typeof (object[]) }, messageCode: "IL2067")]
[Kept]
private static void FromParameterWithNonPublicConstructors (
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.NonPublicConstructors)]
[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
Type type)
{
// Only the explicitly non-public call will work
Activator.CreateInstance (type);
Activator.CreateInstance (type, new object[] { 1 });
Activator.CreateInstance (type, BindingFlags.NonPublic, null, new object[] { 1, 2 }, null);
}
[Kept]
class FromParameterWithNonPublicConstructorsType
{
public FromParameterWithNonPublicConstructorsType () { }
public FromParameterWithNonPublicConstructorsType (int arg) { }
[Kept]
private FromParameterWithNonPublicConstructorsType (int arg, int arg2) { }
}
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (Type), typeof (BindingFlags), typeof (Binder), typeof (object[]), typeof (CultureInfo) }, messageCode: "IL2067")]
[Kept]
private static void FromParameterWithPublicConstructors (
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
[KeptAttributeAttribute(typeof(DynamicallyAccessedMembersAttribute))]
Type type)
{
// Only public ctors and default ctor are required, so only those cases should work
Activator.CreateInstance (type);
Activator.CreateInstance (type, new object[] { 1 });
Activator.CreateInstance (type, BindingFlags.NonPublic, null, new object[] { 1, 2 }, null);
}
[Kept]
class FromParameterWithPublicConstructorsType
{
[Kept]
public FromParameterWithPublicConstructorsType () { }
[Kept]
public FromParameterWithPublicConstructorsType (int arg) { }
private FromParameterWithPublicConstructorsType (int arg, int arg2) { }
}
[Kept]
class WithAssemblyNameParameterless1
{
[Kept]
public WithAssemblyNameParameterless1 ()
{
}
public WithAssemblyNameParameterless1 (int i, object o)
{
}
}
[Kept]
class WithAssemblyNameParameterless2
{
[Kept]
public WithAssemblyNameParameterless2 ()
{
}
public WithAssemblyNameParameterless2 (int i, object o)
{
}
}
[Kept]
class WithAssemblyNamePublicOnly
{
[Kept]
public WithAssemblyNamePublicOnly ()
{
}
[Kept]
public WithAssemblyNamePublicOnly (int i, object o)
{
}
private WithAssemblyNamePublicOnly (int i, object o, int j)
{
}
}
[Kept]
class WithAssemblyNamePrivateOnly
{
public WithAssemblyNamePrivateOnly ()
{
}
[Kept]
private WithAssemblyNamePrivateOnly (int i, object o, int j)
{
}
}
[Kept]
private static BindingFlags GetBindingFlags ()
{
return BindingFlags.Public;
}
[Kept]
private static object[] GetArgs ()
{
return null;
}
[Kept]
private static void WithAssemblyName ()
{
Activator.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNameParameterless1");
Activator.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNameParameterless2", new object[] { });
Activator.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNamePublicOnly", false, BindingFlags.Public, null, new object[] { }, null, new object[] { });
Activator.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNamePrivateOnly", false, BindingFlags.NonPublic, null, new object[] { }, null, new object[] { });
WithNullAssemblyName ();
WithNonExistingAssemblyName ();
WithAssemblyAndUnknownTypeName ();
WithAssemblyAndNonExistingTypeName ();
}
[Kept]
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (string), typeof (string) },
messageCode: "IL2032", message: new string[] { "assemblyName" })]
private static void WithNullAssemblyName ()
{
Activator.CreateInstance (null, "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNameParameterless1");
}
[Kept]
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (string), typeof (string) },
messageCode: "IL2061", message: new string[] { "NonExistingAssembly" })]
private static void WithNonExistingAssemblyName ()
{
Activator.CreateInstance ("NonExistingAssembly", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyNameParameterless1");
}
[Kept]
private static string _typeNameField;
[Kept]
[UnrecognizedReflectionAccessPattern (typeof (Activator), nameof (Activator.CreateInstance), new Type[] { typeof (string), typeof (string), typeof (object[]) },
messageCode: "IL2032", message: new string[] { "typeName" })]
private static void WithAssemblyAndUnknownTypeName ()
{
Activator.CreateInstance ("test", _typeNameField, new object[] { });
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void WithAssemblyAndNonExistingTypeName ()
{
Activator.CreateInstance ("test", "NonExistingType", new object[] { });
}
[Kept]
class WithAssemblyPathParameterless
{
[Kept]
public WithAssemblyPathParameterless ()
{
}
public WithAssemblyPathParameterless (int i, object o)
{
}
}
[Kept]
private static void WithAssemblyPath ()
{
Activator.CreateInstanceFrom ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+WithAssemblyPathParameterless");
}
[Kept]
class AppDomainCreateInstanceType
{
[Kept]
public AppDomainCreateInstanceType ()
{
}
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void AppDomainCreateInstance ()
{
// Just a basic test that these are all recognized, we're not testing that it marks correctly as it has the exact same implementation
// as the above tested Activator.CreateInstance overloads
AppDomain.CurrentDomain.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType");
AppDomain.CurrentDomain.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", new object[] { });
AppDomain.CurrentDomain.CreateInstance ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", false, BindingFlags.Public, null, null, null, null);
AppDomain.CurrentDomain.CreateInstanceAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType");
AppDomain.CurrentDomain.CreateInstanceAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", new object[] { });
AppDomain.CurrentDomain.CreateInstanceAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", false, BindingFlags.Public, null, null, null, null);
AppDomain.CurrentDomain.CreateInstanceFrom ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType");
AppDomain.CurrentDomain.CreateInstanceFrom ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", new object[] { });
AppDomain.CurrentDomain.CreateInstanceFrom ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", false, BindingFlags.Public, null, null, null, null);
AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType");
AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", new object[] { });
AppDomain.CurrentDomain.CreateInstanceFromAndUnwrap ("test", "Mono.Linker.Tests.Cases.Reflection.ActivatorCreateInstance+AppDomainCreateInstanceType", false, BindingFlags.Public, null, null, null, null);
}
[Kept]
[UnrecognizedReflectionAccessPattern (typeof (Assembly), nameof (Assembly.CreateInstance), new Type[] { typeof (string) }, messageCode: "IL2058")]
[UnrecognizedReflectionAccessPattern (typeof (Assembly), nameof (Assembly.CreateInstance), new Type[] { typeof (string), typeof (bool) }, messageCode: "IL2058")]
[UnrecognizedReflectionAccessPattern (typeof (Assembly), nameof (Assembly.CreateInstance), new Type[] { typeof (string), typeof (bool), typeof (BindingFlags), typeof (Binder), typeof (object[]), typeof (CultureInfo), typeof (object[]) }, messageCode: "IL2058")]
private static void UnsupportedCreateInstance ()
{
typeof (ActivatorCreateInstance).Assembly.CreateInstance ("NonExistent");
typeof (ActivatorCreateInstance).Assembly.CreateInstance ("NonExistent", ignoreCase: false);
typeof (ActivatorCreateInstance).Assembly.CreateInstance ("NonExistent", false, BindingFlags.Public, null, new object[] { }, null, new object[] { });
}
[Kept]
class TestCreateInstanceOfTWithConcreteTypeType
{
[Kept]
public TestCreateInstanceOfTWithConcreteTypeType ()
{
}
public TestCreateInstanceOfTWithConcreteTypeType (int i)
{
}
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void TestCreateInstanceOfTWithConcreteType ()
{
Activator.CreateInstance<TestCreateInstanceOfTWithConcreteTypeType> ();
}
[Kept]
class TestCreateInstanceOfTWithNewConstraintType
{
[Kept]
public TestCreateInstanceOfTWithNewConstraintType ()
{
}
public TestCreateInstanceOfTWithNewConstraintType (int i)
{
}
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void TestCreateInstanceOfTWithNewConstraint<T> () where T : new()
{
Activator.CreateInstance<T> ();
}
[Kept]
class TestCreateInstanceOfTWithNoConstraintType
{
public TestCreateInstanceOfTWithNoConstraintType ()
{
}
public TestCreateInstanceOfTWithNoConstraintType (int i)
{
}
}
[Kept]
[ExpectedWarning ("IL2091", nameof (Activator), nameof (Activator.CreateInstance) + "<T>")]
// Warnings are currently duplicated in NETCORE (annotation and intrinsics) - but they're not identical in this case.
// See https://github.com/mono/linker/issues/1483
private static void TestCreateInstanceOfTWithNoConstraint<T> ()
{
Activator.CreateInstance<T> ();
}
[Kept]
class TestCreateInstanceOfTWithDataflowType
{
[Kept]
public TestCreateInstanceOfTWithDataflowType ()
{
}
public TestCreateInstanceOfTWithDataflowType (int i)
{
}
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void TestCreateInstanceOfTWithDataflow<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor),
KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] T> ()
{
Activator.CreateInstance<T> ();
}
[Kept]
class TestNullArgsType
{
[Kept]
public TestNullArgsType () { }
public TestNullArgsType (int i) { }
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void TestNullArgsOnKnownType ()
{
Activator.CreateInstance (typeof (TestNullArgsType), null);
}
[Kept]
[RecognizedReflectionAccessPattern]
private static void TestNullArgsOnAnnotatedType (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor),
KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] Type type)
{
Activator.CreateInstance (type, BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture);
}
[Kept]
[ExpectedWarning ("IL2067", nameof (DynamicallyAccessedMemberTypes) + "." + nameof (DynamicallyAccessedMemberTypes.NonPublicConstructors))]
private static void TestNullArgsNonPublicOnly (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor),
KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] Type type)
{
Activator.CreateInstance (type, BindingFlags.NonPublic | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture);
}
[Kept]
[ExpectedNoWarnings]
private static void TestNullArgsNonPublicWithNonPublicAnnotation (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor | DynamicallyAccessedMemberTypes.NonPublicConstructors),
KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))] Type type)
{
Activator.CreateInstance (type, nonPublic: true);
}
[Kept]
class CreateInstanceWithGetTypeFromHierarchy
{
[Kept]
[KeptAttributeAttribute (typeof (DynamicallyAccessedMembersAttribute))]
[KeptMember (".ctor()")]
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
class AnnotatedBase
{
[Kept]
[ExpectedNoWarnings]
public void TestCreateInstance ()
{
Activator.CreateInstance (GetType (), BindingFlags.Public | BindingFlags.Instance, null, null, CultureInfo.InvariantCulture, null);
}
}
[Kept]
[KeptBaseType (typeof (AnnotatedBase))]
[KeptMember (".ctor()")]
class Derived : AnnotatedBase
{
[Kept]
public static void KeepIt () { }
}
[Kept]
public static void Test ()
{
Derived.KeepIt ();
(new AnnotatedBase ()).TestCreateInstance ();
}
}
[Kept]
[KeptMember (".ctor()")]
class TestType { }
}
}
|