I am using compiled expressions to create instance. It is very fast in JIT but not in AOT (even slower) because of the fallback process. So I want to check whether the code is running in AOT. If yes, I will use ConstructorInfo.Invoke
instead.
ATM, I only have an idea to check this by calling one of the methods are not allowed in AOT and then catch the error. Does any other better ways to check?
Does the System.Runtime.CompilerSerivces.RuntimeFeatures class, with its IsDynamicCodeCompiled and IsDynamicCodeSupported properties, meet the need? It is available in more recent versions of .NET.
From the .NET Standard 2.1 Announcement:
In looking at the .NET source code for Regex, this perhaps might be the check that is triggering the fallback process that was noted, in which case you could then potentially check IsDynamicCodeCompiled yourself and provide your alternate fallback.