Skip to content

Commit 0f97c7a

Browse files
Reflection annotate more of CoreLib (#37418)
1 parent ecc310b commit 0f97c7a

File tree

10 files changed

+64
-1
lines changed

10 files changed

+64
-1
lines changed

src/coreclr/src/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.CoreCLR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,8 @@ public static partial class Marshal
2626
[MethodImpl(MethodImplOptions.InternalCall)]
2727
internal static extern int SizeOfHelper(Type t, bool throwIfNotMarshalable);
2828

29+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
30+
Justification = "Trimming doesn't affect types eligible for marshalling. Different exception for invalid inputs doesn't matter.")]
2931
public static IntPtr OffsetOf(Type t, string fieldName)
3032
{
3133
if (t is null)

src/coreclr/src/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3779,6 +3779,8 @@ private void CreateInstanceCheckThis()
37793779
throw new NotSupportedException(SR.Acc_CreateVoid);
37803780
}
37813781

3782+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
3783+
Justification = "Implementation detail of Activator that linker intrinsically recognizes")]
37823784
internal object? CreateInstanceImpl(
37833785
BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture)
37843786
{

src/coreclr/src/System.Private.CoreLib/src/System/ValueType.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
**
1212
===========================================================*/
1313

14+
using System.Diagnostics.CodeAnalysis;
1415
using System.Reflection;
1516
using System.Runtime.CompilerServices;
1617

@@ -20,6 +21,8 @@ namespace System
2021
[System.Runtime.CompilerServices.TypeForwardedFrom("mscorlib, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089")]
2122
public abstract class ValueType
2223
{
24+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
25+
Justification = "Trimmed fields don't make a difference for equality")]
2326
public override bool Equals(object? obj)
2427
{
2528
if (null == obj)

src/libraries/System.Private.CoreLib/src/System/Activator.RuntimeType.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection;
67
using System.Globalization;
78
using System.Runtime.Loader;
@@ -97,6 +98,8 @@ public static partial class Activator
9798
throw new ArgumentException(SR.Arg_MustBeType, nameof(type));
9899
}
99100

101+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
102+
Justification = "Implementation detail of Activator that linker intrinsically recognizes")]
100103
private static ObjectHandle? CreateInstanceInternal(string assemblyString,
101104
string typeName,
102105
bool ignoreCase,

src/libraries/System.Private.CoreLib/src/System/Activator.cs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Globalization;
78
using System.Reflection;
89
using System.Runtime.Remoting;
@@ -40,12 +41,15 @@ public static partial class Activator
4041
public static object? CreateInstance(Type type) =>
4142
CreateInstance(type, nonPublic: false);
4243

44+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
4345
public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName) =>
4446
CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, null);
4547

48+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
4649
public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes) =>
4750
CreateInstanceFrom(assemblyFile, typeName, false, ConstructorDefault, null, null, null, activationAttributes);
4851

52+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
4953
public static ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, CultureInfo? culture, object?[]? activationAttributes)
5054
{
5155
Assembly assembly = Assembly.LoadFrom(assemblyFile);

src/libraries/System.Private.CoreLib/src/System/AppDomain.cs

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#pragma warning disable CS0067 // events are declared but not used
66

77
using System.Diagnostics;
8+
using System.Diagnostics.CodeAnalysis;
89
using System.IO;
910
using System.Reflection;
1011
using System.Runtime.CompilerServices;
@@ -278,6 +279,7 @@ public void SetThreadPrincipal(IPrincipal principal)
278279
}
279280
}
280281

282+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
281283
public ObjectHandle? CreateInstance(string assemblyName, string typeName)
282284
{
283285
if (assemblyName == null)
@@ -288,6 +290,7 @@ public void SetThreadPrincipal(IPrincipal principal)
288290
return Activator.CreateInstance(assemblyName, typeName);
289291
}
290292

293+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
291294
public ObjectHandle? CreateInstance(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes)
292295
{
293296
if (assemblyName == null)
@@ -305,6 +308,7 @@ public void SetThreadPrincipal(IPrincipal principal)
305308
activationAttributes);
306309
}
307310

311+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
308312
public ObjectHandle? CreateInstance(string assemblyName, string typeName, object?[]? activationAttributes)
309313
{
310314
if (assemblyName == null)
@@ -315,12 +319,14 @@ public void SetThreadPrincipal(IPrincipal principal)
315319
return Activator.CreateInstance(assemblyName, typeName, activationAttributes);
316320
}
317321

322+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
318323
public object? CreateInstanceAndUnwrap(string assemblyName, string typeName)
319324
{
320325
ObjectHandle? oh = CreateInstance(assemblyName, typeName);
321326
return oh?.Unwrap();
322327
}
323328

329+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
324330
public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes)
325331
{
326332
ObjectHandle? oh = CreateInstance(assemblyName,
@@ -334,17 +340,20 @@ public void SetThreadPrincipal(IPrincipal principal)
334340
return oh?.Unwrap();
335341
}
336342

343+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
337344
public object? CreateInstanceAndUnwrap(string assemblyName, string typeName, object?[]? activationAttributes)
338345
{
339346
ObjectHandle? oh = CreateInstance(assemblyName, typeName, activationAttributes);
340347
return oh?.Unwrap();
341348
}
342349

350+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
343351
public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName)
344352
{
345353
return Activator.CreateInstanceFrom(assemblyFile, typeName);
346354
}
347355

356+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
348357
public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes)
349358
{
350359
return Activator.CreateInstanceFrom(assemblyFile,
@@ -357,17 +366,20 @@ public void SetThreadPrincipal(IPrincipal principal)
357366
activationAttributes);
358367
}
359368

369+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
360370
public ObjectHandle? CreateInstanceFrom(string assemblyFile, string typeName, object?[]? activationAttributes)
361371
{
362372
return Activator.CreateInstanceFrom(assemblyFile, typeName, activationAttributes);
363373
}
364374

375+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
365376
public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName)
366377
{
367378
ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName);
368379
return oh?.Unwrap();
369380
}
370381

382+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
371383
public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, bool ignoreCase, BindingFlags bindingAttr, Binder? binder, object?[]? args, System.Globalization.CultureInfo? culture, object?[]? activationAttributes)
372384
{
373385
ObjectHandle? oh = CreateInstanceFrom(assemblyFile,
@@ -381,6 +393,7 @@ public void SetThreadPrincipal(IPrincipal principal)
381393
return oh?.Unwrap();
382394
}
383395

396+
[RequiresUnreferencedCode("Type and its constructor could be removed")]
384397
public object? CreateInstanceFromAndUnwrap(string assemblyFile, string typeName, object?[]? activationAttributes)
385398
{
386399
ObjectHandle? oh = CreateInstanceFrom(assemblyFile, typeName, activationAttributes);

src/libraries/System.Private.CoreLib/src/System/Attribute.cs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
// See the LICENSE file in the project root for more information.
44

55
using System.Diagnostics;
6+
using System.Diagnostics.CodeAnalysis;
67
using System.Reflection;
78

89
namespace System
@@ -15,6 +16,8 @@ public abstract partial class Attribute
1516
protected Attribute() { }
1617

1718
#if !CORERT
19+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
20+
Justification = "Unused fields don't make a difference for equality")]
1821
public override bool Equals(object? obj)
1922
{
2023
if (obj == null)
@@ -47,6 +50,8 @@ public override bool Equals(object? obj)
4750
return true;
4851
}
4952

53+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
54+
Justification = "Unused fields don't make a difference for hashcode quality")]
5055
public override int GetHashCode()
5156
{
5257
Type type = GetType();

src/libraries/System.Private.CoreLib/src/System/Runtime/InteropServices/Marshal.cs

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -509,6 +509,8 @@ public static void Prelink(MethodInfo m)
509509
PrelinkCore(m);
510510
}
511511

512+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
513+
Justification = "This only needs to prelink methods that are actually used")]
512514
public static void PrelinkAll(Type c)
513515
{
514516
if (c is null)
@@ -533,7 +535,9 @@ public static void StructureToPtr<T>([DisallowNull] T structure, IntPtr ptr, boo
533535
/// Creates a new instance of "structuretype" and marshals data from a
534536
/// native memory block to it.
535537
/// </summary>
536-
public static object? PtrToStructure(IntPtr ptr, Type structureType)
538+
public static object? PtrToStructure(IntPtr ptr,
539+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicParameterlessConstructor)]
540+
Type structureType)
537541
{
538542
if (ptr == IntPtr.Zero)
539543
{

src/libraries/System.Private.CoreLib/src/System/Type.Enum.cs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
// The .NET Foundation licenses this file to you under the MIT license.
33
// See the LICENSE file in the project root for more information.
44

5+
using System.Diagnostics.CodeAnalysis;
56
using System.Reflection;
67
using System.Collections;
78
using System.Collections.Generic;
@@ -105,6 +106,8 @@ private Array GetEnumRawConstantValues()
105106
return values;
106107
}
107108

109+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
110+
Justification = "Literal fields on enums can never be trimmed")]
108111
// This will return enumValues and enumNames sorted by the values.
109112
private void GetEnumData(out string[] enumNames, out Array enumValues)
110113
{

src/libraries/System.Private.CoreLib/src/System/Type.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -149,6 +149,8 @@ public ConstructorInfo? TypeInitializer
149149
protected abstract ConstructorInfo? GetConstructorImpl(BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[] types, ParameterModifier[]? modifiers);
150150

151151
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors)]
152+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
153+
Justification = "Linker doesn't recongnize GetConstructors(BindingFlags.Public) but this is what the body is doing")]
152154
public ConstructorInfo[] GetConstructors() => GetConstructors(BindingFlags.Public | BindingFlags.Instance);
153155

154156
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicConstructors | DynamicallyAccessedMemberTypes.NonPublicConstructors)]
@@ -161,6 +163,8 @@ public ConstructorInfo? TypeInitializer
161163
public abstract EventInfo? GetEvent(string name, BindingFlags bindingAttr);
162164

163165
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents)]
166+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
167+
Justification = "Linker doesn't recongnize GetEvents(BindingFlags.Public) but this is what the body is doing")]
164168
public virtual EventInfo[] GetEvents() => GetEvents(Type.DefaultLookup);
165169

166170
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicEvents | DynamicallyAccessedMemberTypes.NonPublicEvents)]
@@ -173,16 +177,26 @@ public ConstructorInfo? TypeInitializer
173177
public abstract FieldInfo? GetField(string name, BindingFlags bindingAttr);
174178

175179
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields)]
180+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
181+
Justification = "Linker doesn't recongnize GetFields(BindingFlags.Public) but this is what the body is doing")]
176182
public FieldInfo[] GetFields() => GetFields(Type.DefaultLookup);
177183

178184
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicFields | DynamicallyAccessedMemberTypes.NonPublicFields)]
179185
public abstract FieldInfo[] GetFields(BindingFlags bindingAttr);
180186

187+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
181188
public MemberInfo[] GetMember(string name) => GetMember(name, Type.DefaultLookup);
189+
190+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
182191
public virtual MemberInfo[] GetMember(string name, BindingFlags bindingAttr) => GetMember(name, MemberTypes.All, bindingAttr);
192+
193+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
183194
public virtual MemberInfo[] GetMember(string name, MemberTypes type, BindingFlags bindingAttr) => throw new NotSupportedException(SR.NotSupported_SubclassOverride);
184195

196+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
185197
public MemberInfo[] GetMembers() => GetMembers(Type.DefaultLookup);
198+
199+
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.All)]
186200
public abstract MemberInfo[] GetMembers(BindingFlags bindingAttr);
187201

188202
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
@@ -253,6 +267,8 @@ public ConstructorInfo? TypeInitializer
253267
protected virtual MethodInfo? GetMethodImpl(string name, int genericParameterCount, BindingFlags bindingAttr, Binder? binder, CallingConventions callConvention, Type[]? types, ParameterModifier[]? modifiers) => throw new NotSupportedException();
254268

255269
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods)]
270+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
271+
Justification = "Linker doesn't recongnize GetMethods(BindingFlags.Public) but this is what the body is doing")]
256272
public MethodInfo[] GetMethods() => GetMethods(Type.DefaultLookup);
257273

258274
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicMethods | DynamicallyAccessedMemberTypes.NonPublicMethods)]
@@ -265,6 +281,8 @@ public ConstructorInfo? TypeInitializer
265281
public abstract Type? GetNestedType(string name, BindingFlags bindingAttr);
266282

267283
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes)]
284+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
285+
Justification = "Linker doesn't recongnize GetNestedTypes(BindingFlags.Public) but this is what the body is doing")]
268286
public Type[] GetNestedTypes() => GetNestedTypes(Type.DefaultLookup);
269287

270288
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicNestedTypes | DynamicallyAccessedMemberTypes.NonPublicNestedTypes)]
@@ -282,6 +300,8 @@ public ConstructorInfo? TypeInitializer
282300
}
283301

284302
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
303+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
304+
Justification = "Linker doesn't recongnize GetPropertyImpl(BindingFlags.Public) but this is what the body is doing")]
285305
public PropertyInfo? GetProperty(string name, Type? returnType)
286306
{
287307
if (name == null)
@@ -312,6 +332,8 @@ public ConstructorInfo? TypeInitializer
312332
protected abstract PropertyInfo? GetPropertyImpl(string name, BindingFlags bindingAttr, Binder? binder, Type? returnType, Type[]? types, ParameterModifier[]? modifiers);
313333

314334
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties)]
335+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
336+
Justification = "Linker doesn't recongnize GetProperties(BindingFlags.Public) but this is what the body is doing")]
315337
public PropertyInfo[] GetProperties() => GetProperties(Type.DefaultLookup);
316338

317339
[DynamicallyAccessedMembers(DynamicallyAccessedMemberTypes.PublicProperties | DynamicallyAccessedMemberTypes.NonPublicProperties)]
@@ -392,6 +414,8 @@ protected virtual TypeCode GetTypeCodeImpl()
392414
public virtual bool IsInstanceOfType([NotNullWhen(true)] object? o) => o == null ? false : IsAssignableFrom(o.GetType());
393415
public virtual bool IsEquivalentTo([NotNullWhen(true)] Type? other) => this == other;
394416

417+
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2006:UnrecognizedReflectionPattern",
418+
Justification = "The single instance field on enum types is never trimmed")]
395419
public virtual Type GetEnumUnderlyingType()
396420
{
397421
if (!IsEnum)

0 commit comments

Comments
 (0)