@@ -151,45 +151,90 @@ public static void Test ()
151
151
class AnnotatedMethodReturnValue
152
152
{
153
153
[ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ]
154
- public static Type MethodWithAnnotatedReturnValue ( ) => null ;
154
+ public static Type StaticMethodWithAnnotatedReturnValue ( ) => null ;
155
155
156
- [ ExpectedWarning ( "IL2111" , nameof ( MethodWithAnnotatedReturnValue ) ) ]
157
- static void Reflection ( )
156
+ [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ]
157
+ public Type InstanceMethodWithAnnotatedReturnValue ( ) => null ;
158
+
159
+ [ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ]
160
+ public virtual Type VirtualMethodWithAnnotatedReturnValue ( ) => null ;
161
+
162
+ // Only virtual methods should warn - the problem is only possible if something overrides a virtual method.
163
+ // Getting an annotated value in itself is not dangerous in any way.
164
+
165
+ static void ReflectionOnStatic ( )
158
166
{
159
- typeof ( AnnotatedMethodReturnValue ) . GetMethod ( nameof ( MethodWithAnnotatedReturnValue ) ) . Invoke ( null , null ) ;
167
+ typeof ( AnnotatedMethodReturnValue ) . GetMethod ( nameof ( StaticMethodWithAnnotatedReturnValue ) ) . Invoke ( null , null ) ;
160
168
}
161
169
162
- [ ExpectedWarning ( "IL2111" , nameof ( MethodWithAnnotatedReturnValue ) ) ]
170
+ static void ReflectionOnInstance ( )
171
+ {
172
+ typeof ( AnnotatedMethodReturnValue ) . GetMethod ( nameof ( InstanceMethodWithAnnotatedReturnValue ) ) . Invoke ( null , null ) ;
173
+ }
174
+
175
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualMethodWithAnnotatedReturnValue ) ) ]
176
+ static void ReflectionOnVirtual ( )
177
+ {
178
+ typeof ( AnnotatedMethodReturnValue ) . GetMethod ( nameof ( VirtualMethodWithAnnotatedReturnValue ) ) . Invoke ( null , null ) ;
179
+ }
180
+
181
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualMethodWithAnnotatedReturnValue ) ) ]
163
182
[ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicMethods , typeof ( AnnotatedMethodReturnValue ) ) ]
164
183
static void DynamicDependency ( )
165
184
{
166
185
}
167
186
168
- [ ExpectedWarning ( "IL2111" , nameof ( MethodWithAnnotatedReturnValue ) ) ]
169
- [ DynamicDependency ( nameof ( MethodWithAnnotatedReturnValue ) , typeof ( AnnotatedMethodReturnValue ) ) ]
170
- static void DynamicDependencyByName ( )
187
+ [ DynamicDependency ( nameof ( StaticMethodWithAnnotatedReturnValue ) , typeof ( AnnotatedMethodReturnValue ) ) ]
188
+ static void DynamicDependencyByNameOnStatic ( )
189
+ {
190
+ }
191
+
192
+ [ DynamicDependency ( nameof ( InstanceMethodWithAnnotatedReturnValue ) , typeof ( AnnotatedMethodReturnValue ) ) ]
193
+ static void DynamicDependencyByNameOnInstance ( )
194
+ {
195
+ }
196
+
197
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualMethodWithAnnotatedReturnValue ) ) ]
198
+ [ DynamicDependency ( nameof ( VirtualMethodWithAnnotatedReturnValue ) , typeof ( AnnotatedMethodReturnValue ) ) ]
199
+ static void DynamicDependencyByNameOnVirtual ( )
171
200
{
172
201
}
173
202
174
- [ ExpectedWarning ( "IL2111" , nameof ( MethodWithAnnotatedReturnValue ) ) ]
203
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualMethodWithAnnotatedReturnValue ) ) ]
175
204
static void DynamicallyAccessedMembers ( )
176
205
{
177
206
typeof ( AnnotatedMethodReturnValue ) . RequiresPublicMethods ( ) ;
178
207
}
179
208
180
- [ ExpectedWarning ( "IL2111" , nameof ( MethodWithAnnotatedReturnValue ) ) ]
181
- static void Ldftn ( )
209
+ static void LdftnOnStatic ( )
182
210
{
183
- var _ = new Func < Type > ( AnnotatedMethodReturnValue . MethodWithAnnotatedReturnValue ) ;
211
+ var _ = new Func < Type > ( AnnotatedMethodReturnValue . StaticMethodWithAnnotatedReturnValue ) ;
212
+ }
213
+
214
+ static void LdftnOnInstance ( )
215
+ {
216
+ var _ = new Func < Type > ( ( new AnnotatedMethodReturnValue ( ) ) . InstanceMethodWithAnnotatedReturnValue ) ;
217
+ }
218
+
219
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualMethodWithAnnotatedReturnValue ) ) ]
220
+ static void LdftnOnVirtual ( )
221
+ {
222
+ var _ = new Func < Type > ( ( new AnnotatedMethodReturnValue ( ) ) . VirtualMethodWithAnnotatedReturnValue ) ;
184
223
}
185
224
186
225
public static void Test ( )
187
226
{
188
- Reflection ( ) ;
227
+ ReflectionOnStatic ( ) ;
228
+ ReflectionOnInstance ( ) ;
229
+ ReflectionOnVirtual ( ) ;
189
230
DynamicDependency ( ) ;
190
- DynamicDependencyByName ( ) ;
231
+ DynamicDependencyByNameOnStatic ( ) ;
232
+ DynamicDependencyByNameOnInstance ( ) ;
233
+ DynamicDependencyByNameOnVirtual ( ) ;
191
234
DynamicallyAccessedMembers ( ) ;
192
- Ldftn ( ) ;
235
+ LdftnOnStatic ( ) ;
236
+ LdftnOnInstance ( ) ;
237
+ LdftnOnVirtual ( ) ;
193
238
}
194
239
}
195
240
@@ -201,6 +246,9 @@ class AnnotatedProperty
201
246
[ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicEvents ) ]
202
247
public static Type PropertyWithAnnotationGetterOnly { get => null ; }
203
248
249
+ [ DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicEvents ) ]
250
+ public virtual Type VirtualPropertyWithAnnotationGetterOnly { get => null ; }
251
+
204
252
class AttributeWithPropertyWithAnnotation : Attribute
205
253
{
206
254
public AttributeWithPropertyWithAnnotation ( ) { }
@@ -212,21 +260,20 @@ public AttributeWithPropertyWithAnnotation () { }
212
260
[ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) ) ]
213
261
static void ReflectionOnPropertyItself ( )
214
262
{
215
- // TODO: Technically this should warn if one of the setter is annotated since
216
- // linker can't guarantee that it will not be used.
217
263
typeof ( AnnotatedProperty ) . GetProperty ( nameof ( PropertyWithAnnotation ) ) ;
218
264
}
219
265
220
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotationGetterOnly ) ) ]
221
266
static void ReflectionOnPropertyWithGetterOnly ( )
222
267
{
223
- // Following the rules we maintain on normal methods, just returning annotated value is considered dangerous
224
- // (in theory one could use type builder to create an override for the method, and its body would not be validated
225
- // and would need to fulfill the annotation on the return value anyway)
226
268
typeof ( AnnotatedProperty ) . GetProperty ( nameof ( PropertyWithAnnotationGetterOnly ) ) ;
227
269
}
228
270
229
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) + ".get" ) ]
271
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualPropertyWithAnnotationGetterOnly ) ) ]
272
+ static void ReflectionOnPropertyWithGetterOnlyOnVirtual ( )
273
+ {
274
+ typeof ( AnnotatedProperty ) . GetProperty ( nameof ( VirtualPropertyWithAnnotationGetterOnly ) ) ;
275
+ }
276
+
230
277
static void ReflectionOnGetter ( )
231
278
{
232
279
typeof ( AnnotatedProperty ) . GetMethod ( "get_" + nameof ( PropertyWithAnnotation ) ) ;
@@ -238,23 +285,27 @@ static void ReflectionOnSetter ()
238
285
typeof ( AnnotatedProperty ) . GetMethod ( "set_" + nameof ( PropertyWithAnnotation ) ) ;
239
286
}
240
287
288
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualPropertyWithAnnotationGetterOnly ) + ".get" ) ]
289
+ static void ReflectionOnVirtualGetter ( )
290
+ {
291
+ typeof ( AnnotatedProperty ) . GetMethod ( "get_" + nameof ( VirtualPropertyWithAnnotationGetterOnly ) ) ;
292
+ }
293
+
241
294
// Should not warn - there's nothing wrong with this
242
295
[ AttributeWithPropertyWithAnnotation ( PropertyWithAnnotation = typeof ( TestType ) ) ]
243
296
static void AnnotatedAttributeProperty ( )
244
297
{
245
298
}
246
299
247
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) + ".get" ) ]
248
300
[ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) + ".set" ) ]
249
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotationGetterOnly ) + ".get" ) ]
301
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualPropertyWithAnnotationGetterOnly ) + ".get" ) ]
250
302
[ DynamicDependency ( DynamicallyAccessedMemberTypes . PublicProperties , typeof ( AnnotatedProperty ) ) ]
251
303
static void DynamicDependency ( )
252
304
{
253
305
}
254
306
255
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) + ".get" ) ]
256
307
[ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotation ) + ".set" ) ]
257
- [ ExpectedWarning ( "IL2111" , nameof ( PropertyWithAnnotationGetterOnly ) + ".get" ) ]
308
+ [ ExpectedWarning ( "IL2111" , nameof ( VirtualPropertyWithAnnotationGetterOnly ) + ".get" ) ]
258
309
static void DynamicallyAccessedMembers ( )
259
310
{
260
311
typeof ( AnnotatedProperty ) . RequiresPublicProperties ( ) ;
@@ -264,8 +315,10 @@ public static void Test ()
264
315
{
265
316
ReflectionOnPropertyItself ( ) ;
266
317
ReflectionOnPropertyWithGetterOnly ( ) ;
318
+ ReflectionOnPropertyWithGetterOnlyOnVirtual ( ) ;
267
319
ReflectionOnGetter ( ) ;
268
320
ReflectionOnSetter ( ) ;
321
+ ReflectionOnVirtualGetter ( ) ;
269
322
AnnotatedAttributeProperty ( ) ;
270
323
DynamicDependency ( ) ;
271
324
DynamicallyAccessedMembers ( ) ;
@@ -393,7 +446,7 @@ public static void Test ()
393
446
394
447
class AccessThroughLdToken
395
448
{
396
- static Type PropertyWithLdToken {
449
+ public virtual Type PropertyWithLdToken {
397
450
[ return : DynamicallyAccessedMembers ( DynamicallyAccessedMemberTypes . PublicMethods ) ]
398
451
get {
399
452
return null ;
@@ -403,7 +456,7 @@ static Type PropertyWithLdToken {
403
456
[ ExpectedWarning ( "IL2111" , nameof ( PropertyWithLdToken ) ) ]
404
457
public static void Test ( )
405
458
{
406
- Expression < Func < Type > > getter = ( ) => PropertyWithLdToken ;
459
+ Expression < Func < Type > > getter = ( ) => ( new AccessThroughLdToken ( ) ) . PropertyWithLdToken ;
407
460
}
408
461
}
409
462
0 commit comments