diff --git a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs index e16d76377cd8f4..9ef7a07a79321a 100644 --- a/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs +++ b/src/coreclr/System.Private.CoreLib/src/System/Array.CoreCLR.cs @@ -315,6 +315,7 @@ private unsafe bool IsValueOfElementType(object value) // if this is an array of value classes and that value class has a default constructor // then this calls this default constructor on every element in the value class array. // otherwise this is a no-op. Generally this method is called automatically by the compiler + [RequiresUnreferencedCode("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")] public unsafe void Initialize() { MethodTable* pArrayMT = RuntimeHelpers.GetMethodTable(this); diff --git a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Array.NativeAot.cs b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Array.NativeAot.cs index ce137a254b02d5..10a20705637f55 100644 --- a/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Array.NativeAot.cs +++ b/src/coreclr/nativeaot/System.Private.CoreLib/src/System/Array.NativeAot.cs @@ -253,6 +253,7 @@ internal static unsafe Array CtorRare(MethodTable* pEEType, int nDimensions, int } } + [RequiresUnreferencedCode("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")] public unsafe void Initialize() { MethodTable* pElementEEType = ElementMethodTable; diff --git a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/PrimitiveDataContract.cs b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/PrimitiveDataContract.cs index 36fccdf7f0b006..d52445f3b5d8b3 100644 --- a/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/PrimitiveDataContract.cs +++ b/src/libraries/System.Private.DataContractSerialization/src/System/Runtime/Serialization/PrimitiveDataContract.cs @@ -921,6 +921,7 @@ internal NMTOKENSDataContract() : base(DictionaryGlobals.NMTOKENSLocalName, Dict internal sealed class ByteArrayDataContract : PrimitiveDataContract { + [RequiresUnreferencedCode("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")] public ByteArrayDataContract() : base(typeof(byte[]), DictionaryGlobals.ByteArrayLocalName, DictionaryGlobals.SchemaNamespace) { } diff --git a/src/libraries/System.Runtime/ref/System.Runtime.cs b/src/libraries/System.Runtime/ref/System.Runtime.cs index 76900e576f7a78..3cb55d8062d45b 100644 --- a/src/libraries/System.Runtime/ref/System.Runtime.cs +++ b/src/libraries/System.Runtime/ref/System.Runtime.cs @@ -574,6 +574,7 @@ public static void ForEach(T[] array, System.Action action) { } public static int IndexOf(T[] array, T value) { throw null; } public static int IndexOf(T[] array, T value, int startIndex) { throw null; } public static int IndexOf(T[] array, T value, int startIndex, int count) { throw null; } + [System.Diagnostics.CodeAnalysis.RequiresUnreferencedCodeAttribute("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")] public void Initialize() { } public static int LastIndexOf(System.Array array, object? value) { throw null; } public static int LastIndexOf(System.Array array, object? value, int startIndex) { throw null; } diff --git a/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs b/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs index e7cec206975d8f..76daa0d97333c3 100644 --- a/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs +++ b/src/mono/System.Private.CoreLib/src/System/Array.Mono.cs @@ -347,6 +347,7 @@ internal void InternalSetValue(object? value, nint index) SetValueImpl(ObjectHandleOnStack.Create(ref self), ObjectHandleOnStack.Create(ref value), (int)index); } + [RequiresUnreferencedCode("The default constructor of the array's element type may be trimmed if not referenced elsewhere.")] public void Initialize() { object arr = this;