/// <remarks> /// This is the entry point to the expression compilation system. The system /// a) Will rewrite the expression to avoid null refs when any part of the expression tree is evaluated to null /// b) Attempt to cache the result, or an intermediate part of the result. /// If the provided expression is particularly obscure and the system doesn't know how to handle it, it will /// return null. /// </remarks> /// <returns> /// FROM: https://github.com/dotnet/aspnetcore/blob/v3.1.11/src/Mvc/Mvc.ViewFeatures/src/CachedExpressionCompiler.cs /// </returns> public static Func <TModel, object> Process <TModel, TResult>( Expression <Func <TModel, TResult> > expression) { if (expression == null) { throw new ArgumentNullException(nameof(expression)); } return(CachedExpressionCompiler.Process(expression)); }
private static void InsertIndexerInvocationText( StringBuilder builder, Expression indexExpression, LambdaExpression parentExpression) { if (builder == null) { throw new ArgumentNullException(nameof(builder)); } if (indexExpression == null) { throw new ArgumentNullException(nameof(indexExpression)); } if (parentExpression == null) { throw new ArgumentNullException(nameof(parentExpression)); } if (parentExpression.Parameters == null) { throw new ArgumentException(string.Format("The '{0}' property of '{1}' must not be null.", nameof(parentExpression.Parameters), nameof(parentExpression))); } var converted = Expression.Convert(indexExpression, typeof(object)); var fakeParameter = Expression.Parameter(typeof(object), null); var lambda = Expression.Lambda <Func <object, object> >(converted, fakeParameter); Func <object, object> func; try { func = CachedExpressionCompiler.Process(lambda) ?? lambda.Compile(); } catch (InvalidOperationException ex) { var parameters = parentExpression.Parameters.ToArray(); throw new InvalidOperationException( string.Format("The expression compiler was unable to evaluate the indexer expression '{0}' because it references the model parameter '{1}' which is unavailable.", indexExpression, parameters[0].Name), ex); } builder.Insert(0, ']'); builder.Insert(0, Convert.ToString(func(null), CultureInfo.InvariantCulture)); builder.Insert(0, '['); }