diff --git a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
index bf676b7b6f26be..230e4cb4276a42 100644
--- a/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
+++ b/src/libraries/Common/tests/TestUtilities/System/AssertExtensions.cs
@@ -3,6 +3,7 @@
using System.Collections.Generic;
using System.Linq;
+using System.Runtime.CompilerServices;
using System.Runtime.InteropServices;
using System.Threading;
using System.Threading.Tasks;
@@ -15,6 +16,26 @@ public static class AssertExtensions
{
private static bool IsNetFramework => RuntimeInformation.FrameworkDescription.StartsWith(".NET Framework");
+
+ ///
+ /// Helper for AOT tests that verifies that the compile succeeds, or throws PlatformNotSupported
+ /// when AOT is enabled.
+ ///
+ public static void ThrowsOnAot(Action action)
+ where T : Exception
+ {
+#if NETCOREAPP // Dynamic code is always supported on .NET Framework
+ if (!RuntimeFeature.IsDynamicCodeSupported)
+ {
+ Assert.Throws(action);
+ }
+ else
+#endif
+ {
+ action();
+ }
+ }
+
public static void Throws(Action action, string expectedMessage)
where T : Exception
{
diff --git a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/Expressions/ExpressionResolverBuilder.cs b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/Expressions/ExpressionResolverBuilder.cs
index c28b9f00bb4e7d..670c62ee2f69a8 100644
--- a/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/Expressions/ExpressionResolverBuilder.cs
+++ b/src/libraries/Microsoft.Extensions.DependencyInjection/src/ServiceLookup/Expressions/ExpressionResolverBuilder.cs
@@ -123,6 +123,15 @@ static MethodInfo GetArrayEmptyMethodInfo(Type elementType)
return ServiceLookupHelpers.GetArrayEmptyMethodInfo(elementType);
}
+ [UnconditionalSuppressMessage("AotAnalysis", "IL3050:RequiresDynamicCode",
+ Justification = "VerifyAotCompatibility ensures elementType is not a ValueType")]
+ static NewArrayExpression NewArrayInit(Type elementType, IEnumerable expr)
+ {
+ Debug.Assert(!ServiceProvider.VerifyAotCompatibility || !elementType.IsValueType, "VerifyAotCompatibility=true will throw during building the IEnumerableCallSite if elementType is a ValueType.");
+
+ return Expression.NewArrayInit(elementType, expr);
+ }
+
if (callSite.ServiceCallSites.Length == 0)
{
return Expression.Constant(
@@ -130,7 +139,7 @@ static MethodInfo GetArrayEmptyMethodInfo(Type elementType)
.Invoke(obj: null, parameters: Array.Empty