/// <summary> /// Prepares function <see cref="System.Func{Arg1T,Arg2T,Arg3T,Arg4T,ResultT}"/> with specified signature for execution in AOT compiled environment. /// </summary> /// <typeparam name="Arg1T">Function's first argument.</typeparam> /// <typeparam name="Arg2T">Function's second argument.</typeparam> /// <typeparam name="Arg3T">Function's third argument.</typeparam> /// <typeparam name="Arg4T">Function's fourth argument.</typeparam> /// <typeparam name="ResultT">Function result type.</typeparam> public static void RegisterFunc <Arg1T, Arg2T, Arg3T, Arg4T, ResultT>() { if (typeof(AotCompilation).Name == string.Empty) { // ReSharper disable once AssignNullToNotNullAttribute var fn = Expression.Lambda <Func <Arg1T, Arg2T, Arg3T, Arg4T, ResultT> >(default(Expression), default(ParameterExpression[])).CompileAot(); fn.Invoke(default(Arg1T), default(Arg2T), default(Arg3T), default(Arg4T)); fn.DynamicInvoke(default(Arg1T), default(Arg2T), default(Arg3T), default(Arg4T)); AotCompiler.Prepare <Arg1T, Arg2T, Arg3T, Arg4T, ResultT>(default(Expression), default(ReadOnlyCollection <ParameterExpression>)); } }
/// <summary> /// Prepares function <see cref="System.Func{ResultT}"/> with specified signature for execution in AOT compiled environment. /// </summary> /// <typeparam name="ResultT">Function result type.</typeparam> public static void RegisterFunc <ResultT>() { if (typeof(AotCompilation).Name == string.Empty) { // ReSharper disable once AssignNullToNotNullAttribute var fn = Expression.Lambda <Func <ResultT> >(default(Expression), default(ParameterExpression[])).CompileAot(); fn.Invoke(); fn.DynamicInvoke(); AotCompiler.Prepare <ResultT>(default(Expression)); } }
/// <summary> /// Compiles specified expression into <see cref="Func{TArg1, TArg2, TResult}"/> delegate using AOT aware expression compiler. /// </summary> /// <typeparam name="TArg1">First argument type.</typeparam> /// <typeparam name="TArg2">Second argument type.</typeparam> /// <typeparam name="TResult">Result type.</typeparam> /// <param name="expression">An expression syntax tree. Not null.</param> /// <param name="forceAot">True to always use AOT compiler event if environment is JIT and supports dynamic code.</param> /// <returns>A compiled expression.</returns> public static Func <TArg1, TArg2, TResult> CompileAot <TArg1, TArg2, TResult>(this Expression <Func <TArg1, TArg2, TResult> > expression, bool forceAot = false) { if (expression == null) { throw new ArgumentNullException("expression"); } AotCompilation.RegisterFunc <TArg1, TArg2, TResult>(); if (AotCompilation.IsAotRuntime || forceAot) { return(AotCompiler.Prepare <TArg1, TArg2, TResult>(expression.Body, expression.Parameters)); } else { return(expression.Compile()); } }