Respect IsDynamicCodeSupported in more places in Linq.Expressions (#88539)
authorEric Erhardt <eric.erhardt@microsoft.com>
Tue, 11 Jul 2023 16:55:13 +0000 (11:55 -0500)
committerGitHub <noreply@github.com>
Tue, 11 Jul 2023 16:55:13 +0000 (11:55 -0500)
commitd2fae906418b604ec8c2f032cc127c379c3e5f54
tree3b049948608b1eb2b09ba6032362e49aa09b64c1
parentf98389c500d31839cf56d32a75eb2ddccab48441
Respect IsDynamicCodeSupported in more places in Linq.Expressions (#88539)

* Respect IsDynamicCodeSupported in more places in Linq.Expressions

* CanEmitObjectArrayDelegate
* CanCreateArbitraryDelegates

These properties are all set to `false` when running on NativeAOT, so have them respect RuntimeFeature.IsDynamicCodeSupported.

However, CanEmitObjectArrayDelegate needs a work around because DynamicDelegateAugments.CreateObjectArrayDelegate does not exist in CoreClr's System.Private.CoreLib.

Allow System.Linq.Expressions to create an object[] delegate using Ref.Emit even though RuntimeFeature.IsDynamicCodeSupported is set to false (ex. using a feature switch). To enable this, add an internal method in CoreLib that temporarily allows the current thread to skip the RuntimeFeature check and allows DynamicMethod instances to be created. When System.Linq.Expressions needs to generate one of these delegates, it calls the internal method through Reflection and continues to use Ref.Emit to generate the delegate.

Fix #81803
src/libraries/System.Linq.Expressions/src/System/Dynamic/Utils/DelegateHelpers.cs
src/libraries/System.Linq.Expressions/src/System/Linq/Expressions/Interpreter/CallInstruction.cs
src/libraries/System.Linq.Expressions/tests/CompilerTests.cs
src/libraries/System.Private.CoreLib/src/ILLink/ILLink.Descriptors.LibraryBuild.xml
src/libraries/System.Private.CoreLib/src/System/Reflection/Emit/AssemblyBuilder.cs