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
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
|
using System;
using System.Collections.Generic;
using System.Diagnostics.CodeAnalysis;
using System.Reflection;
using System.Security.Policy;
using System.Text;
using Mono.Linker.Tests.Cases.Expectations.Assertions;
using Mono.Linker.Tests.Cases.Expectations.Helpers;
using BindingFlags = System.Reflection.BindingFlags;
namespace Mono.Linker.Tests.Cases.DataFlow
{
[SkipKeptItemsValidation]
[ExpectedNoWarnings]
public class GenericParameterDataFlow
{
public static void Main ()
{
TestSingleGenericParameterOnType ();
TestMultipleGenericParametersOnType ();
TestBaseTypeGenericRequirements ();
TestDeepNestedTypesWithGenerics ();
TestInterfaceTypeGenericRequirements ();
TestTypeGenericRequirementsOnMembers ();
TestPartialInstantiationTypes ();
TestSingleGenericParameterOnMethod ();
TestMultipleGenericParametersOnMethod ();
TestMethodGenericParametersViaInheritance ();
MakeGenericType.Test ();
MakeGenericMethod.Test ();
TestNewConstraintSatisfiesParameterlessConstructor<object> ();
TestStructConstraintSatisfiesParameterlessConstructor<TestStruct> ();
TestUnmanagedConstraintSatisfiesParameterlessConstructor<byte> ();
TestGenericParameterFlowsToField ();
TestGenericParameterFlowsToReturnValue ();
}
static void TestSingleGenericParameterOnType ()
{
TypeRequiresNothing<TestType>.Test ();
TypeRequiresPublicFields<TestType>.Test ();
TypeRequiresPublicMethods<TestType>.Test ();
TypeRequiresPublicFieldsPassThrough<TestType>.Test ();
TypeRequiresNothingPassThrough<TestType>.Test ();
}
static void TestGenericParameterFlowsToField ()
{
TypeRequiresPublicFields<TestType>.TestFields ();
}
static void TestGenericParameterFlowsToReturnValue ()
{
_ = TypeRequiresPublicFields<TestType>.ReturnRequiresPublicFields ();
_ = TypeRequiresPublicFields<TestType>.ReturnRequiresPublicMethods ();
_ = TypeRequiresPublicFields<TestType>.ReturnRequiresNothing ();
}
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
static Type FieldRequiresPublicFields;
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
static Type FieldRequiresPublicMethods;
static Type FieldRequiresNothing;
class TypeRequiresPublicFields<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
{
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) },
messageCode: "IL2087", message: new string[] {
nameof (T),
nameof (TypeRequiresPublicFields <T>),
nameof (DataFlowTypeExtensions.RequiresPublicMethods)
})]
public static void Test ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (GenericParameterDataFlow), nameof (FieldRequiresPublicMethods),
messageCode: "IL2089", message: new string[] {
nameof (T),
nameof (TypeRequiresPublicFields <T>),
nameof (FieldRequiresPublicMethods)
})]
public static void TestFields ()
{
FieldRequiresPublicFields = typeof (T);
FieldRequiresPublicMethods = typeof (T);
FieldRequiresNothing = typeof (T);
}
[RecognizedReflectionAccessPattern]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)]
public static Type ReturnRequiresPublicFields ()
{
return typeof (T);
}
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicFields<>), nameof (ReturnRequiresPublicMethods), new Type[] { }, returnType: typeof (Type),
messageCode: "IL2088", message: new string[] {
nameof (T),
nameof (TypeRequiresPublicFields<T>),
nameof (ReturnRequiresPublicMethods)
})]
[return: DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]
public static Type ReturnRequiresPublicMethods ()
{
return typeof (T);
}
public static Type ReturnRequiresNothing ()
{
return typeof (T);
}
}
class TypeRequiresPublicMethods<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T>
{
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
public static void Test ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
}
class TypeRequiresNothing<T>
{
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
public static void Test ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
}
class TypeRequiresPublicFieldsPassThrough<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TSource>
{
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T",
messageCode: "IL2091", message: new string[] {
nameof(TSource),
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeRequiresPublicFieldsPassThrough<TSource>",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeRequiresPublicMethods<T>" })]
public static void Test ()
{
TypeRequiresPublicFields<TSource>.Test ();
TypeRequiresPublicMethods<TSource>.Test ();
TypeRequiresNothing<TSource>.Test ();
}
}
class TypeRequiresNothingPassThrough<T>
{
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicFields<>), "T", messageCode: "IL2091")]
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
public static void Test ()
{
TypeRequiresPublicFields<T>.Test ();
TypeRequiresPublicMethods<T>.Test ();
TypeRequiresNothing<T>.Test ();
}
}
static void TestMultipleGenericParametersOnType ()
{
MultipleTypesWithDifferentRequirements<TestType, TestType, TestType, TestType>.TestMultiple ();
MultipleTypesWithDifferentRequirements<TestType, TestType, TestType, TestType>.TestFields ();
MultipleTypesWithDifferentRequirements<TestType, TestType, TestType, TestType>.TestMethods ();
MultipleTypesWithDifferentRequirements<TestType, TestType, TestType, TestType>.TestBoth ();
MultipleTypesWithDifferentRequirements<TestType, TestType, TestType, TestType>.TestNothing ();
}
class MultipleTypesWithDifferentRequirements<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing>
{
[RecognizedReflectionAccessPattern]
public static void TestMultiple ()
{
typeof (TFields).RequiresPublicFields ();
typeof (TMethods).RequiresPublicMethods ();
typeof (TBoth).RequiresPublicFields ();
typeof (TBoth).RequiresPublicMethods ();
typeof (TFields).RequiresNone ();
typeof (TMethods).RequiresNone ();
typeof (TBoth).RequiresNone ();
typeof (TNothing).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
public static void TestFields ()
{
typeof (TFields).RequiresPublicFields ();
typeof (TFields).RequiresPublicMethods ();
typeof (TFields).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
public static void TestMethods ()
{
typeof (TMethods).RequiresPublicFields ();
typeof (TMethods).RequiresPublicMethods ();
typeof (TMethods).RequiresNone ();
}
[RecognizedReflectionAccessPattern]
public static void TestBoth ()
{
typeof (TBoth).RequiresPublicFields ();
typeof (TBoth).RequiresPublicMethods ();
typeof (TBoth).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
public static void TestNothing ()
{
typeof (TNothing).RequiresPublicFields ();
typeof (TNothing).RequiresPublicMethods ();
typeof (TNothing).RequiresNone ();
}
}
[RecognizedReflectionAccessPattern]
static void TestBaseTypeGenericRequirements ()
{
new DerivedTypeWithInstantiatedGenericOnBase ();
new DerivedTypeWithOpenGenericOnBase<TestType> ();
new DerivedTypeWithOpenGenericOnBaseWithRequirements<TestType> ();
}
class GenericBaseTypeWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
{
[RecognizedReflectionAccessPattern]
public GenericBaseTypeWithRequirements ()
{
typeof (T).RequiresPublicFields ();
}
}
[RecognizedReflectionAccessPattern]
class DerivedTypeWithInstantiatedGenericOnBase : GenericBaseTypeWithRequirements<TestType>
{
}
[UnrecognizedReflectionAccessPattern (typeof (GenericBaseTypeWithRequirements<>), "T", messageCode: "IL2091")]
class DerivedTypeWithOpenGenericOnBase<T> : GenericBaseTypeWithRequirements<T>
{
}
[RecognizedReflectionAccessPattern]
class DerivedTypeWithOpenGenericOnBaseWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
: GenericBaseTypeWithRequirements<T>
{
}
static void TestInterfaceTypeGenericRequirements ()
{
IGenericInterfaceTypeWithRequirements<TestType> instance = new InterfaceImplementationTypeWithInstantiatedGenericOnBase ();
new InterfaceImplementationTypeWithOpenGenericOnBase<TestType> ();
new InterfaceImplementationTypeWithOpenGenericOnBaseWithRequirements<TestType> ();
}
interface IGenericInterfaceTypeWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
{
}
[RecognizedReflectionAccessPattern]
class InterfaceImplementationTypeWithInstantiatedGenericOnBase : IGenericInterfaceTypeWithRequirements<TestType>
{
}
[UnrecognizedReflectionAccessPattern (typeof (IGenericInterfaceTypeWithRequirements<>), "T", messageCode: "IL2091")]
class InterfaceImplementationTypeWithOpenGenericOnBase<T> : IGenericInterfaceTypeWithRequirements<T>
{
}
[RecognizedReflectionAccessPattern]
class InterfaceImplementationTypeWithOpenGenericOnBaseWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
: IGenericInterfaceTypeWithRequirements<T>
{
}
[RecognizedReflectionAccessPattern]
static void TestDeepNestedTypesWithGenerics ()
{
RootTypeWithRequirements<TestType>.InnerTypeWithNoAddedGenerics.TestAccess ();
}
class RootTypeWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TRoot>
{
public class InnerTypeWithNoAddedGenerics
{
// The message is not ideal since we report the TRoot to come from RootTypeWithRequirements/InnerTypeWIthNoAddedGenerics
// while it originates on RootTypeWithRequirements, but it's correct from IL's point of view.
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) },
messageCode: "IL2087", message: new string[] {
nameof(TRoot),
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.RootTypeWithRequirements<TRoot>.InnerTypeWithNoAddedGenerics",
"type",
"DataFlowTypeExtensions.RequiresPublicMethods(Type)" })]
public static void TestAccess ()
{
typeof (TRoot).RequiresPublicFields ();
typeof (TRoot).RequiresPublicMethods ();
}
}
}
[RecognizedReflectionAccessPattern]
static void TestTypeGenericRequirementsOnMembers ()
{
// Basically just root everything we need to test
var instance = new TypeGenericRequirementsOnMembers<TestType> ();
_ = instance.PublicFieldsField;
_ = instance.PublicMethodsField;
_ = instance.PublicFieldsProperty;
instance.PublicFieldsProperty = null;
_ = instance.PublicMethodsProperty;
instance.PublicMethodsProperty = null;
instance.PublicFieldsMethodParameter (null);
instance.PublicMethodsMethodParameter (null);
instance.PublicFieldsMethodReturnValue ();
instance.PublicMethodsMethodReturnValue ();
instance.PublicFieldsMethodLocalVariable ();
instance.PublicMethodsMethodLocalVariable ();
}
class TypeGenericRequirementsOnMembers<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TOuter>
{
[RecognizedReflectionAccessPattern]
public TypeRequiresPublicFields<TOuter> PublicFieldsField;
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
public TypeRequiresPublicMethods<TOuter> PublicMethodsField;
public TypeRequiresPublicFields<TOuter> PublicFieldsProperty {
[RecognizedReflectionAccessPattern]
get;
[RecognizedReflectionAccessPattern]
set;
}
public TypeRequiresPublicMethods<TOuter> PublicMethodsProperty {
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
get;
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
set;
}
[RecognizedReflectionAccessPattern]
public void PublicFieldsMethodParameter (TypeRequiresPublicFields<TOuter> param) { }
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
public void PublicMethodsMethodParameter (TypeRequiresPublicMethods<TOuter> param) { }
[RecognizedReflectionAccessPattern]
public TypeRequiresPublicFields<TOuter> PublicFieldsMethodReturnValue () { return null; }
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")] // Return value
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")] // Compiler generated local variable
public TypeRequiresPublicMethods<TOuter> PublicMethodsMethodReturnValue () { return null; }
[RecognizedReflectionAccessPattern]
public void PublicFieldsMethodLocalVariable ()
{
TypeRequiresPublicFields<TOuter> t = null;
}
[UnrecognizedReflectionAccessPattern (typeof (TypeRequiresPublicMethods<>), "T", messageCode: "IL2091")]
public void PublicMethodsMethodLocalVariable ()
{
TypeRequiresPublicMethods<TOuter> t = null;
}
}
[RecognizedReflectionAccessPattern]
static void TestPartialInstantiationTypes ()
{
_ = new PartialyInstantiatedFields<TestType> ();
_ = new FullyInstantiatedOverPartiallyInstantiatedFields ();
_ = new PartialyInstantiatedMethods<TestType> ();
_ = new FullyInstantiatedOverPartiallyInstantiatedMethods ();
}
class BaseForPartialInstantiation<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods>
{
}
[RecognizedReflectionAccessPattern]
class PartialyInstantiatedFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TOuter>
: BaseForPartialInstantiation<TOuter, TestType>
{
}
[RecognizedReflectionAccessPattern]
class FullyInstantiatedOverPartiallyInstantiatedFields
: PartialyInstantiatedFields<TestType>
{
}
[UnrecognizedReflectionAccessPattern (typeof (BaseForPartialInstantiation<,>), "TMethods", messageCode: "IL2091")]
class PartialyInstantiatedMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TOuter>
: BaseForPartialInstantiation<TestType, TOuter>
{
}
[RecognizedReflectionAccessPattern]
class FullyInstantiatedOverPartiallyInstantiatedMethods
: PartialyInstantiatedMethods<TestType>
{
}
static void TestSingleGenericParameterOnMethod ()
{
MethodRequiresPublicFields<TestType> ();
MethodRequiresPublicMethods<TestType> ();
MethodRequiresNothing<TestType> ();
MethodRequiresPublicFieldsPassThrough<TestType> ();
MethodRequiresNothingPassThrough<TestType> ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodRequiresPublicFields<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodRequiresPublicMethods<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodRequiresNothing<T> ()
{
typeof (T).RequiresPublicFields ();
typeof (T).RequiresPublicMethods ();
typeof (T).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (GenericParameterDataFlow), nameof (MethodRequiresPublicMethods) + "<T>()::T", messageCode: "IL2091")]
static void MethodRequiresPublicFieldsPassThrough<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
MethodRequiresPublicFields<T> ();
MethodRequiresPublicMethods<T> ();
MethodRequiresNothing<T> ();
}
[UnrecognizedReflectionAccessPattern (typeof (GenericParameterDataFlow), nameof (MethodRequiresPublicFields) + "<T>()::T", messageCode: "IL2091")]
[UnrecognizedReflectionAccessPattern (typeof (GenericParameterDataFlow), nameof (MethodRequiresPublicMethods) + "<T>()::T", messageCode: "IL2091")]
static void MethodRequiresNothingPassThrough<T> ()
{
MethodRequiresPublicFields<T> ();
MethodRequiresPublicMethods<T> ();
MethodRequiresNothing<T> ();
}
static void TestMethodGenericParametersViaInheritance ()
{
TypeWithInstantiatedGenericMethodViaGenericParameter<TestType>.StaticRequiresPublicFields<TestType> ();
TypeWithInstantiatedGenericMethodViaGenericParameter<TestType>.StaticRequiresPublicFieldsNonGeneric ();
TypeWithInstantiatedGenericMethodViaGenericParameter<TestType>.StaticPartialInstantiation ();
TypeWithInstantiatedGenericMethodViaGenericParameter<TestType>.StaticPartialInstantiationUnrecognized ();
var instance = new TypeWithInstantiatedGenericMethodViaGenericParameter<TestType> ();
instance.InstanceRequiresPublicFields<TestType> ();
instance.InstanceRequiresPublicFieldsNonGeneric ();
instance.VirtualRequiresPublicFields<TestType> ();
instance.VirtualRequiresPublicMethods<TestType> ();
instance.CallInterface ();
IInterfaceWithGenericMethod interfaceInstance = (IInterfaceWithGenericMethod) instance;
interfaceInstance.InterfaceRequiresPublicFields<TestType> ();
interfaceInstance.InterfaceRequiresPublicMethods<TestType> ();
}
class BaseTypeWithGenericMethod
{
[RecognizedReflectionAccessPattern]
public static void StaticRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
=> typeof (T).RequiresPublicFields ();
[RecognizedReflectionAccessPattern]
public void InstanceRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
=> typeof (T).RequiresPublicFields ();
[RecognizedReflectionAccessPattern]
public virtual void VirtualRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
=> typeof (T).RequiresPublicFields ();
[RecognizedReflectionAccessPattern]
public static void StaticRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
=> typeof (T).RequiresPublicMethods ();
[RecognizedReflectionAccessPattern]
public void InstanceRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]T> ()
=> typeof (T).RequiresPublicMethods ();
[RecognizedReflectionAccessPattern]
public virtual void VirtualRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)]T> ()
=> typeof (T).RequiresPublicMethods ();
[RecognizedReflectionAccessPattern]
public static void StaticRequiresMultipleGenericParams<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods> ()
{
typeof (TFields).RequiresPublicFields ();
typeof (TMethods).RequiresPublicMethods ();
}
}
interface IInterfaceWithGenericMethod
{
void InterfaceRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ();
void InterfaceRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ();
}
class TypeWithInstantiatedGenericMethodViaGenericParameter<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TOuter>
: BaseTypeWithGenericMethod, IInterfaceWithGenericMethod
{
[UnrecognizedReflectionAccessPattern (typeof (BaseTypeWithGenericMethod), nameof (BaseTypeWithGenericMethod.StaticRequiresPublicMethods) + "<T>()::T",
messageCode: "IL2091", message: new string[] {
"TInner",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>.StaticRequiresPublicFields<TInner>()",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.BaseTypeWithGenericMethod.StaticRequiresPublicMethods<T>()" })]
public static void StaticRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TInner> ()
{
StaticRequiresPublicFields<TInner> ();
StaticRequiresPublicMethods<TInner> ();
}
[UnrecognizedReflectionAccessPattern (typeof (BaseTypeWithGenericMethod), nameof (BaseTypeWithGenericMethod.StaticRequiresPublicMethods) + "<T>()::T",
messageCode: "IL2091", message: new string[] {
"TOuter",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.BaseTypeWithGenericMethod.StaticRequiresPublicMethods<T>()" })]
public static void StaticRequiresPublicFieldsNonGeneric ()
{
StaticRequiresPublicFields<TOuter> ();
StaticRequiresPublicMethods<TOuter> ();
}
[RecognizedReflectionAccessPattern]
public static void StaticPartialInstantiation ()
{
StaticRequiresMultipleGenericParams<TOuter, TestType> ();
}
[UnrecognizedReflectionAccessPattern (typeof (BaseTypeWithGenericMethod), nameof (BaseTypeWithGenericMethod.StaticRequiresMultipleGenericParams) + "<TFields,TMethods>()::TMethods",
messageCode: "IL2091", message: new string[] {
"TOuter",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>",
"TMethods",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.BaseTypeWithGenericMethod.StaticRequiresMultipleGenericParams<TFields,TMethods>()" })]
public static void StaticPartialInstantiationUnrecognized ()
{
StaticRequiresMultipleGenericParams<TestType, TOuter> ();
}
[UnrecognizedReflectionAccessPattern (typeof (BaseTypeWithGenericMethod), nameof (BaseTypeWithGenericMethod.InstanceRequiresPublicMethods) + "<T>()::T",
messageCode: "IL2091", message: new string[] {
"TInner",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>.InstanceRequiresPublicFields<TInner>()",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.BaseTypeWithGenericMethod.InstanceRequiresPublicMethods<T>()" })]
public void InstanceRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TInner> ()
{
InstanceRequiresPublicFields<TInner> ();
InstanceRequiresPublicMethods<TInner> ();
}
[UnrecognizedReflectionAccessPattern (typeof (BaseTypeWithGenericMethod), nameof (BaseTypeWithGenericMethod.InstanceRequiresPublicMethods) + "<T>()::T",
messageCode: "IL2091", message: new string[] {
"TOuter",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.BaseTypeWithGenericMethod.InstanceRequiresPublicMethods<T>()" })]
public void InstanceRequiresPublicFieldsNonGeneric ()
{
InstanceRequiresPublicFields<TOuter> ();
InstanceRequiresPublicMethods<TOuter> ();
}
[RecognizedReflectionAccessPattern]
public override void VirtualRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
typeof (T).RequiresPublicFields ();
}
[RecognizedReflectionAccessPattern]
public override void VirtualRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
{
typeof (T).RequiresPublicMethods ();
}
[RecognizedReflectionAccessPattern]
public void InterfaceRequiresPublicFields<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
typeof (T).RequiresPublicFields (); ;
}
[RecognizedReflectionAccessPattern]
public void InterfaceRequiresPublicMethods<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] T> ()
{
typeof (T).RequiresPublicMethods ();
}
[UnrecognizedReflectionAccessPattern (typeof (IInterfaceWithGenericMethod), nameof (IInterfaceWithGenericMethod.InterfaceRequiresPublicMethods) + "<T>()::T",
messageCode: "IL2091", message: new string[] {
"TOuter",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.TypeWithInstantiatedGenericMethodViaGenericParameter<TOuter>",
"T",
"Mono.Linker.Tests.Cases.DataFlow.GenericParameterDataFlow.IInterfaceWithGenericMethod.InterfaceRequiresPublicMethods<T>()" })]
public void CallInterface ()
{
IInterfaceWithGenericMethod interfaceInstance = (IInterfaceWithGenericMethod) this;
interfaceInstance.InterfaceRequiresPublicFields<TOuter> ();
interfaceInstance.InterfaceRequiresPublicMethods<TOuter> ();
}
}
static void TestMultipleGenericParametersOnMethod ()
{
MethodMultipleWithDifferentRequirements_TestMultiple<TestType, TestType, TestType, TestType> ();
MethodMultipleWithDifferentRequirements_TestFields<TestType, TestType, TestType, TestType> ();
MethodMultipleWithDifferentRequirements_TestMethods<TestType, TestType, TestType, TestType> ();
MethodMultipleWithDifferentRequirements_TestBoth<TestType, TestType, TestType, TestType> ();
MethodMultipleWithDifferentRequirements_TestNothing<TestType, TestType, TestType, TestType> ();
}
[RecognizedReflectionAccessPattern]
static void MethodMultipleWithDifferentRequirements_TestMultiple<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing> ()
{
typeof (TFields).RequiresPublicFields (); ;
typeof (TMethods).RequiresPublicMethods ();
typeof (TBoth).RequiresPublicFields (); ;
typeof (TBoth).RequiresPublicMethods ();
typeof (TFields).RequiresNone ();
typeof (TMethods).RequiresNone ();
typeof (TBoth).RequiresNone ();
typeof (TNothing).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodMultipleWithDifferentRequirements_TestFields<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing> ()
{
typeof (TFields).RequiresPublicFields (); ;
typeof (TFields).RequiresPublicMethods ();
typeof (TFields).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodMultipleWithDifferentRequirements_TestMethods<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing> ()
{
typeof (TMethods).RequiresPublicFields ();
typeof (TMethods).RequiresPublicMethods ();
typeof (TMethods).RequiresNone ();
}
static void MethodMultipleWithDifferentRequirements_TestBoth<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing> ()
{
typeof (TBoth).RequiresPublicFields ();
typeof (TBoth).RequiresPublicMethods ();
typeof (TBoth).RequiresNone ();
}
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicFields), new Type[] { typeof (Type) }, messageCode: "IL2087")]
[UnrecognizedReflectionAccessPattern (typeof (DataFlowTypeExtensions), nameof (DataFlowTypeExtensions.RequiresPublicMethods), new Type[] { typeof (Type) }, messageCode: "IL2087")]
static void MethodMultipleWithDifferentRequirements_TestNothing<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TFields,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TMethods,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.PublicMethods)] TBoth,
TNothing> ()
{
typeof (TNothing).RequiresPublicFields ();
typeof (TNothing).RequiresPublicMethods ();
typeof (TNothing).RequiresNone ();
}
class MakeGenericType
{
public static void Test ()
{
TestNullType ();
TestUnknownInput (null);
TestWithUnknownTypeArray (null);
TestWithArrayUnknownIndexSet (0);
TestWithArrayUnknownLengthSet (1);
TestNoArguments ();
TestWithRequirements ();
TestWithRequirementsFromParam (null);
TestWithRequirementsFromGenericParam<TestType> ();
TestWithNoRequirements ();
TestWithNoRequirementsFromParam (null);
TestWithMultipleArgumentsWithRequirements ();
TestWithNewConstraint ();
TestWithStructConstraint ();
TestWithUnmanagedConstraint ();
TestWithNullable ();
}
// This is OK since we know it's null, so MakeGenericType is effectively a no-op (will throw)
// so no validation necessary.
[RecognizedReflectionAccessPattern]
static void TestNullType ()
{
Type nullType = null;
nullType.MakeGenericType (typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (Type), nameof (Type.MakeGenericType), new Type[] { typeof (Type[]) },
messageCode: "IL2055")]
static void TestUnknownInput (Type inputType)
{
inputType.MakeGenericType (typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (Type), nameof (Type.MakeGenericType), new Type[] { typeof (Type[]) },
messageCode: "IL2055")]
static void TestWithUnknownTypeArray (Type[] types)
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (types);
}
[UnrecognizedReflectionAccessPattern (typeof (Type), nameof (Type.MakeGenericType), new Type[] { typeof (Type[]) },
messageCode: "IL2055")]
static void TestWithArrayUnknownIndexSet (int indexToSet)
{
Type[] types = new Type[1];
types[indexToSet] = typeof (TestType);
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (types);
}
[UnrecognizedReflectionAccessPattern (typeof (Type), nameof (Type.MakeGenericType), new Type[] { typeof (Type[]) },
messageCode: "IL2055")]
static void TestWithArrayUnknownLengthSet (int arrayLen)
{
Type[] types = new Type[arrayLen];
types[0] = typeof (TestType);
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (types);
}
[RecognizedReflectionAccessPattern]
static void TestNoArguments ()
{
typeof (TypeMakeGenericNoArguments).MakeGenericType ();
}
class TypeMakeGenericNoArguments
{
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirements ()
{
// Currently this is not analyzable since we don't track array elements.
// Would be really nice to support this kind of code in the future though.
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (typeof (TestType));
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirementsFromParam (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type)
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (type);
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirementsFromGenericParam<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
typeof (GenericWithPublicFieldsArgument<>).MakeGenericType (typeof (T));
}
class GenericWithPublicFieldsArgument<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T>
{
}
[RecognizedReflectionAccessPattern]
static void TestWithNoRequirements ()
{
typeof (GenericWithNoRequirements<>).MakeGenericType (typeof (TestType));
}
[RecognizedReflectionAccessPattern]
static void TestWithNoRequirementsFromParam (Type type)
{
typeof (GenericWithNoRequirements<>).MakeGenericType (type);
}
class GenericWithNoRequirements<T>
{
}
[RecognizedReflectionAccessPattern]
static void TestWithMultipleArgumentsWithRequirements ()
{
typeof (GenericWithMultipleArgumentsWithRequirements<,>).MakeGenericType (typeof (TestType), typeof (TestType));
}
class GenericWithMultipleArgumentsWithRequirements<
TOne,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicMethods)] TTwo>
{
}
[RecognizedReflectionAccessPattern]
static void TestWithNewConstraint ()
{
typeof (GenericWithNewConstraint<>).MakeGenericType (typeof (TestType));
}
class GenericWithNewConstraint<T> where T : new()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithStructConstraint ()
{
typeof (GenericWithStructConstraint<>).MakeGenericType (typeof (TestType));
}
class GenericWithStructConstraint<T> where T : struct
{
}
[RecognizedReflectionAccessPattern]
static void TestWithUnmanagedConstraint ()
{
typeof (GenericWithUnmanagedConstraint<>).MakeGenericType (typeof (TestType));
}
class GenericWithUnmanagedConstraint<T> where T : unmanaged
{
}
[RecognizedReflectionAccessPattern]
static void TestWithNullable ()
{
typeof (Nullable<>).MakeGenericType (typeof (TestType));
}
}
class MakeGenericMethod
{
public static void Test ()
{
TestNullMethod ();
TestUnknownMethod (null);
TestUnknownMethodButNoTypeArguments (null);
TestWithUnknownTypeArray (null);
TestWithArrayUnknownIndexSet (0);
TestWithArrayUnknownIndexSetByRef (0);
TestWithArrayUnknownLengthSet (1);
TestWithArrayPassedToAnotherMethod ();
TestWithNoArguments ();
TestWithRequirements ();
TestWithRequirementsFromParam (null);
TestWithRequirementsFromGenericParam<TestType> ();
TestWithRequirementsViaRuntimeMethod ();
TestWithRequirementsButNoTypeArguments ();
TestWithMultipleKnownGenericParameters ();
TestWithOneUnknownGenericParameter (null);
TestWithPartiallyInitializedGenericTypeArray ();
TestWithConditionalGenericTypeSet (true);
TestWithNoRequirements ();
TestWithNoRequirementsFromParam (null);
TestWithNoRequirementsViaRuntimeMethod ();
TestWithMultipleArgumentsWithRequirements ();
TestWithNewConstraint ();
TestWithStructConstraint ();
TestWithUnmanagedConstraint ();
}
[RecognizedReflectionAccessPattern]
static void TestNullMethod ()
{
MethodInfo mi = null;
mi.MakeGenericMethod (typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestUnknownMethod (MethodInfo mi)
{
mi.MakeGenericMethod (typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestUnknownMethodButNoTypeArguments (MethodInfo mi)
{
// Thechnically linker could figure this out, but it's not worth the complexity - such call will always fail at runtime.
mi.MakeGenericMethod (Type.EmptyTypes);
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithUnknownTypeArray (Type[] types)
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (types);
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithArrayUnknownIndexSet (int indexToSet)
{
Type[] types = new Type[1];
types[indexToSet] = typeof (TestType);
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (types);
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithArrayUnknownIndexSetByRef (int indexToSet)
{
Type[] types = new Type[1];
types[0] = typeof (TestType);
ref Type t = ref types[indexToSet];
t = typeof (TestType);
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (types);
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithArrayUnknownLengthSet (int arrayLen)
{
Type[] types = new Type[arrayLen];
types[0] = typeof (TestType);
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (types);
}
static void MethodThatTakesArrayParameter (Type[] types)
{
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithArrayPassedToAnotherMethod ()
{
Type[] types = new Type[1];
types[0] = typeof (TestType);
MethodThatTakesArrayParameter (types);
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (types);
}
[RecognizedReflectionAccessPattern]
static void TestWithNoArguments ()
{
typeof (MakeGenericMethod).GetMethod (nameof (NonGenericMethod), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod ();
}
static void NonGenericMethod ()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirements ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (typeof (TestType));
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirementsFromParam (
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] Type type)
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (type);
}
static void TestWithRequirementsFromGenericParam<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (typeof (T));
}
[RecognizedReflectionAccessPattern]
static void TestWithRequirementsViaRuntimeMethod ()
{
typeof (MakeGenericMethod).GetRuntimeMethod (nameof (GenericWithRequirements), Type.EmptyTypes)
.MakeGenericMethod (typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithRequirementsButNoTypeArguments ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithRequirements), BindingFlags.Static)
.MakeGenericMethod (Type.EmptyTypes);
}
public static void GenericWithRequirements<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T> ()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithMultipleKnownGenericParameters ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericMultipleParameters), BindingFlags.Static)
.MakeGenericMethod (typeof (TestType), typeof (TestType), typeof (TestType));
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithOneUnknownGenericParameter (Type[] types)
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericMultipleParameters), BindingFlags.Static)
.MakeGenericMethod (typeof (TestType), typeof (TestStruct), types[0]);
}
[UnrecognizedReflectionAccessPattern (typeof (MethodInfo), nameof (MethodInfo.MakeGenericMethod), new Type[] { typeof (Type[]) },
messageCode: "IL2060")]
static void TestWithPartiallyInitializedGenericTypeArray ()
{
Type[] types = new Type[3];
types[0] = typeof (TestType);
types[1] = typeof (TestStruct);
typeof (MakeGenericMethod).GetMethod (nameof (GenericMultipleParameters), BindingFlags.Static)
.MakeGenericMethod (types);
}
[RecognizedReflectionAccessPattern]
static void TestWithConditionalGenericTypeSet (bool thirdParameterIsStruct)
{
Type[] types = new Type[3];
types[0] = typeof (TestType);
types[1] = typeof (TestStruct);
if (thirdParameterIsStruct) {
types[2] = typeof (TestStruct);
} else {
types[2] = typeof (TestType);
}
typeof (MakeGenericMethod).GetMethod (nameof (GenericMultipleParameters), BindingFlags.Static)
.MakeGenericMethod (types);
}
public static void GenericMultipleParameters<
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] T,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] U,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] V> ()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithNoRequirements ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithNoRequirements), BindingFlags.Static)
.MakeGenericMethod (typeof (TestType));
}
[RecognizedReflectionAccessPattern]
static void TestWithNoRequirementsFromParam (Type type)
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithNoRequirements), BindingFlags.Static)
.MakeGenericMethod (type);
}
[RecognizedReflectionAccessPattern]
static void TestWithNoRequirementsViaRuntimeMethod ()
{
typeof (MakeGenericMethod).GetRuntimeMethod (nameof (GenericWithNoRequirements), Type.EmptyTypes)
.MakeGenericMethod (typeof (TestType));
}
public static void GenericWithNoRequirements<T> ()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithMultipleArgumentsWithRequirements ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithMultipleArgumentsWithRequirements), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod (typeof (TestType), typeof (TestType));
}
static void GenericWithMultipleArgumentsWithRequirements<
TOne,
[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicFields)] TTwo> ()
{
}
[RecognizedReflectionAccessPattern]
static void TestWithNewConstraint ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithNewConstraint), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod (typeof (TestType));
}
static void GenericWithNewConstraint<T> () where T : new()
{
var t = new T ();
}
[RecognizedReflectionAccessPattern]
static void TestWithStructConstraint ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithStructConstraint), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod (typeof (TestType));
}
static void GenericWithStructConstraint<T> () where T : struct
{
var t = new T ();
}
[RecognizedReflectionAccessPattern]
static void TestWithUnmanagedConstraint ()
{
typeof (MakeGenericMethod).GetMethod (nameof (GenericWithUnmanagedConstraint), BindingFlags.Static | BindingFlags.NonPublic)
.MakeGenericMethod (typeof (TestType));
}
static void GenericWithUnmanagedConstraint<T> () where T : unmanaged
{
var t = new T ();
}
}
[RecognizedReflectionAccessPattern]
static void TestNewConstraintSatisfiesParameterlessConstructor<T> () where T : new()
{
RequiresParameterlessConstructor<T> ();
}
[RecognizedReflectionAccessPattern]
static void TestStructConstraintSatisfiesParameterlessConstructor<T> () where T : struct
{
RequiresParameterlessConstructor<T> ();
}
[RecognizedReflectionAccessPattern]
static void TestUnmanagedConstraintSatisfiesParameterlessConstructor<T> () where T : unmanaged
{
RequiresParameterlessConstructor<T> ();
}
static void RequiresParameterlessConstructor<[DynamicallyAccessedMembers (DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)] T> ()
{
}
public class TestType
{
}
public struct TestStruct
{
}
}
}
|