diff --git a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
index c84e918d843035..46e7a1448a6670 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Attribute.CoreCLR.cs
@@ -434,6 +434,7 @@ private static AttributeUsageAttribute InternalGetAttributeUsage(Type type)
SR.Format(SR.Format_AttributeUsage, type));
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static Attribute[] CreateAttributeArrayHelper(Type elementType, int elementCount) =>
elementType.ContainsGenericParameters ? new Attribute[elementCount] : (Attribute[])Array.CreateInstance(elementType, elementCount);
#endregion
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Collections/Generic/ComparerHelpers.cs b/src/coreclr/System.Private.CoreLib/src/System/Collections/Generic/ComparerHelpers.cs
index 6812a6c92faf26..e2094ab6ab9eca 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Collections/Generic/ComparerHelpers.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Collections/Generic/ComparerHelpers.cs
@@ -2,6 +2,7 @@
// The .NET Foundation licenses this file to you under the MIT license.
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
using static System.RuntimeTypeHandle;
namespace System.Collections.Generic
@@ -25,6 +26,8 @@ internal static class ComparerHelpers
/// The logic in this method is replicated in vm/compile.cpp to ensure that NGen saves the right instantiations,
/// and in vm/jitinterface.cpp so the jit can model the behavior of this method.
///
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is safe in this method")]
internal static object CreateDefaultComparer(Type type)
{
Debug.Assert(type != null && type is RuntimeType);
@@ -59,6 +62,8 @@ internal static object CreateDefaultComparer(Type type)
/// Creates the default for a nullable type.
///
/// The nullable type to create the default comparer for.
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is safe in this method")]
private static object? TryCreateNullableComparer(RuntimeType nullableType)
{
Debug.Assert(nullableType != null);
@@ -113,6 +118,8 @@ internal static object CreateDefaultComparer(Type type)
///
/// The logic in this method is replicated in vm/compile.cpp to ensure that NGen saves the right instantiations.
///
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is safe in this method")]
internal static object CreateDefaultEqualityComparer(Type type)
{
Debug.Assert(type != null && type is RuntimeType);
@@ -157,6 +164,8 @@ internal static object CreateDefaultEqualityComparer(Type type)
/// Creates the default for a nullable type.
///
/// The nullable type to create the default equality comparer for.
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is safe in this method")]
private static object? TryCreateNullableEqualityComparer(RuntimeType nullableType)
{
Debug.Assert(nullableType != null);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs
index 655f018c24f139..8d09f22023119b 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/MethodBuilderInstantiation.cs
@@ -79,6 +79,7 @@ public override bool ContainsGenericParameters
}
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
public override MethodInfo MakeGenericMethod(params Type[] arguments)
{
throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericMethodDefinition, this));
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
index 735956362086dd..e7f36944fdc7c2 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/SymbolType.cs
@@ -275,11 +275,13 @@ public override Type MakeByRefType()
return SymbolType.FormCompoundType(m_format + "&", m_baseType, 0)!;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType(m_format + "[]", m_baseType, 0)!;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
string s = GetRankString(rank);
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
index 2f0bc41b4d3029..dbf17b7d1ba965 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilder.cs
@@ -55,6 +55,8 @@ public void Bake(ModuleBuilder module, int token)
#region Public Static Methods
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static MethodInfo GetMethod(Type type, MethodInfo method)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
@@ -90,6 +92,8 @@ public static MethodInfo GetMethod(Type type, MethodInfo method)
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2055:UnrecognizedReflectionPattern",
Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
public static ConstructorInfo GetConstructor(Type type, ConstructorInfo constructor)
{
if (type is not TypeBuilder && type is not TypeBuilderInstantiation)
@@ -1461,6 +1465,8 @@ public ConstructorBuilder DefineDefaultConstructor(MethodAttributes attributes)
Justification = "MakeGenericType is only called on a TypeBuilderInstantiation which is not subject to trimming")]
[UnconditionalSuppressMessage("ReflectionAnalysis", "IL2075:UnrecognizedReflectionPattern",
Justification = "GetConstructor is only called on a TypeBuilderInstantiation which is not subject to trimming")]
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "MakeGenericType is only called on a TypeBuilder which is not subject to trimming")]
private ConstructorBuilder DefineDefaultConstructorNoLock(MethodAttributes attributes)
{
ConstructorBuilder constBuilder;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
index 4e195408633ece..89236c475b0461 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/TypeBuilderInstantiation.cs
@@ -79,10 +79,12 @@ public override Type MakeByRefType()
{
return SymbolType.FormCompoundType("&", this, 0)!;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType()
{
return SymbolType.FormCompoundType("[]", this, 0)!;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
if (rank <= 0)
@@ -109,6 +111,11 @@ public override Type MakeArrayType(int rank)
"Currently this is not supported by linker. Once it is supported the outercall (Type.MakeGenericType)" +
"will validate that the types fullfill the necessary requirements of annotations on type parameters." +
"As such the actual internals of the implementation are not interesting.")]
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "The entire TypeBuilderInstantiation is serving the MakeGenericType implementation. " +
+ "Currently this is not supported by linker. Once it is supported the outercall (Type.MakeGenericType)" +
+ "will validate that the types fullfill the necessary requirements of annotations on type parameters." +
+ "As such the actual internals of the implementation are not interesting.")]
private Type Substitute(Type[] substitutes)
{
Type[] inst = GetGenericArguments();
@@ -251,6 +258,7 @@ public override bool ContainsGenericParameters
public override Type GetGenericTypeDefinition() { return m_type; }
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
public override Type MakeGenericType(params Type[] inst) { throw new InvalidOperationException(SR.Format(SR.Arg_NotGenericTypeDefinition, this)); }
public override bool IsAssignableFrom([NotNullWhen(true)] Type? c) { throw new NotSupportedException(); }
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs
index 1841a8cfe0ff1e..292cbd30dd8dab 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/Emit/XXXOnTypeBuilderInstantiation.cs
@@ -62,6 +62,7 @@ public override object Invoke(object? obj, BindingFlags invokeAttr, Binder? bind
public override bool IsGenericMethodDefinition => m_method.IsGenericMethodDefinition;
public override bool ContainsGenericParameters => m_method.ContainsGenericParameters;
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
public override MethodInfo MakeGenericMethod(params Type[] typeArgs)
{
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
index d28edee2403814..ffd6005eafff45 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeCustomAttributeData.cs
@@ -557,6 +557,7 @@ private static RuntimeType ResolveType(RuntimeModule scope, string typeName)
}
#endregion
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
internal CustomAttributeTypedArgument(RuntimeModule scope, CustomAttributeEncodedArgument encodedArg)
{
CustomAttributeEncoding encodedType = encodedArg.CustomAttributeType.EncodedType;
@@ -1433,6 +1434,7 @@ internal static AttributeUsageAttribute GetAttributeUsage(RuntimeType decoratedA
return attributeUsageAttribute ?? AttributeUsageAttribute.Default;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
internal static object[] CreateAttributeArrayHelper(RuntimeType caType, int elementCount)
{
bool useAttributeArray = false;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
index 2b26b90cb7770f..3cafc42ec62235 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/Reflection/RuntimeMethodInfo.CoreCLR.cs
@@ -434,6 +434,7 @@ private Delegate CreateDelegateInternal(Type delegateType!!, object? firstArgume
#region Generics
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
public override MethodInfo MakeGenericMethod(params Type[] methodInstantiation!!)
{
RuntimeType[] methodInstantionRuntimeType = new RuntimeType[methodInstantiation.Length];
diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
index 811772e39d004c..c516bd312b5380 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeHandles.cs
@@ -40,6 +40,7 @@ internal RuntimeType GetTypeChecked()
internal static extern bool IsInstanceOfType(RuntimeType type, [NotNullWhen(true)] object? o);
[RequiresUnreferencedCode("MakeGenericType cannot be statically analyzed. It's not possible to guarantee the availability of requirements of the generic type.")]
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
internal static Type GetTypeHelper(Type typeStart, Type[]? genericArgs, IntPtr pModifiers, int cModifiers)
{
Type type = typeStart;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
index 56589cf6b79387..ae75b121c6a527 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/RuntimeType.CoreCLR.cs
@@ -1015,6 +1015,10 @@ private void AddSpecialInterface(
Justification = "Calls to GetInterfaces technically require all interfaces on ReflectedType" +
"But this is not a public API to enumerate reflection items, all the public APIs which do that" +
"should be annotated accordingly.")]
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:UnrecognizedReflectionPattern",
+ Justification = "Calls to GetInterfaces technically require all interfaces on ReflectedType" +
+ "But this is not a public API to enumerate reflection items, all the public APIs which do that" +
+ "should be annotated accordingly.")]
private RuntimeType[] PopulateInterfaces(Filter filter)
{
ListBuilder list = default;
@@ -1601,6 +1605,7 @@ internal TypeCode TypeCode
return m_defaultMemberName;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
internal object[] GetEmptyArray() => _emptyArray ??= (object[])Array.CreateInstance(m_runtimeType, 0);
#endregion
@@ -3336,6 +3341,7 @@ public override Type[] GetGenericArguments()
}
[RequiresUnreferencedCode("If some of the generic arguments are annotated (either with DynamicallyAccessedMembersAttribute, or generic constraints), trimming can't validate that the requirements of those annotations are met.")]
+ [RequiresDynamicCode("The native code for this instantiation might not be available at runtime.")]
public override Type MakeGenericType(Type[] instantiation!!)
{
if (!IsGenericTypeDefinition)
@@ -3438,8 +3444,10 @@ public override Type[] GetGenericParameterConstraints()
public override Type MakeByRefType() => new RuntimeTypeHandle(this).MakeByRef();
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType() => new RuntimeTypeHandle(this).MakeSZArray();
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
public override Type MakeArrayType(int rank)
{
if (rank <= 0)
@@ -3860,6 +3868,7 @@ private extern object InvokeDispMethod(
return ret;
}
+ [RequiresDynamicCode("The code for an array of the specified type might not be available.")]
private static void WrapArgsForInvokeCall(object[] aArgs, int[] aArgsWrapperTypes)
{
int cArgs = aArgs.Length;
diff --git a/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs b/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
index bf1ac24679664d..734500f3d801e6 100644
--- a/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
+++ b/src/coreclr/System.Private.CoreLib/src/System/StubHelpers.cs
@@ -5,6 +5,7 @@
using System.Runtime.InteropServices;
using System.Runtime.CompilerServices;
using System.Diagnostics;
+using System.Diagnostics.CodeAnalysis;
namespace System.StubHelpers
{
@@ -1045,6 +1046,7 @@ internal unsafe void ConvertToManaged(object pManagedHome, IntPtr pNativeHome)
}
}
+ [RequiresDynamicCode("Marshalling code for the object might not be available")]
internal void ClearNative(IntPtr pNativeHome)
{
if (pNativeHome != IntPtr.Zero)
diff --git a/src/libraries/System.Private.CoreLib/src/System/Enum.cs b/src/libraries/System.Private.CoreLib/src/System/Enum.cs
index c0250365c30ab6..b4707f6add65c2 100644
--- a/src/libraries/System.Private.CoreLib/src/System/Enum.cs
+++ b/src/libraries/System.Private.CoreLib/src/System/Enum.cs
@@ -302,6 +302,7 @@ public static Type GetUnderlyingType(Type enumType!!) =>
enumType.GetEnumUnderlyingType();
#if !CORERT
+ [RequiresDynamicCode("It might not be possible to create an array of the enum type at runtime. Use the GetValues overload instead.")]
public static TEnum[] GetValues() where TEnum : struct, Enum =>
(TEnum[])GetValues(typeof(TEnum));
#endif