internal static Expression InvokeConverter(LanguagePrimitives.ConversionData conversion, Expression value, Type resultType, bool debase, Expression formatProvider) { Expression expression; if ((conversion.Rank == ConversionRank.Identity) || (conversion.Rank == ConversionRank.Assignable)) { expression = debase ? Expression.Call(CachedReflectionInfo.PSObject_Base, value) : value; } else { Expression expression2; Expression nullPSObject; if (debase) { expression2 = Expression.Call(CachedReflectionInfo.PSObject_Base, value); nullPSObject = value.Cast(typeof(PSObject)); } else { expression2 = value.Cast(typeof(object)); nullPSObject = ExpressionCache.NullPSObject; } expression = Expression.Call(Expression.Constant(conversion.Converter), conversion.Converter.GetType().GetMethod("Invoke"), new Expression[] { expression2, Expression.Constant(resultType), ExpressionCache.Constant(true), nullPSObject, formatProvider, ExpressionCache.NullTypeTable }); } if (expression.Type.Equals(resultType) || resultType.Equals(typeof(LanguagePrimitives.InternalPSCustomObject))) { return expression; } if (resultType.IsValueType && (Nullable.GetUnderlyingType(resultType) == null)) { return Expression.Unbox(expression, resultType); } return Expression.Convert(expression, resultType); }
internal static Expression InvokeToString(Expression context, Expression target) { if (target.Type.Equals(typeof(string))) { return target; } return Expression.Call(CachedReflectionInfo.PSObject_ToStringParser, context.Cast(typeof(ExecutionContext)), target.Cast(typeof(object))); }
private Expression SafeIndexResult(Expression expr) { ParameterExpression variable = Expression.Parameter(typeof(Exception)); return Expression.TryCatch(expr.Cast(typeof(object)), new CatchBlock[] { Expression.Catch(variable, Expression.Block(Expression.Call(CachedReflectionInfo.CommandProcessorBase_CheckForSevereException, variable), Expression.IfThen(Compiler.IsStrictMode(3, null), Expression.Rethrow()), this.GetNullResult())) }); }
internal static Expression CallSetVariable(Expression variablePath, Expression rhs, Expression attributes = null) { return Expression.Call(CachedReflectionInfo.VariableOps_SetVariableValue, variablePath, rhs.Cast(typeof(object)), _executionContextParameter, attributes ?? ExpressionCache.NullConstant.Cast(typeof(AttributeAst[]))); }
public Expression GenerateCallContains(Expression lhs, Expression rhs, bool ignoreCase) { return Expression.Call( CachedReflectionInfo.ParserOps_ContainsOperatorCompiled, _executionContextParameter, Expression.Constant(CallSite<Func<CallSite, object, IEnumerator>>.Create(PSEnumerableBinder.Get())), Expression.Constant(CallSite<Func<CallSite, object, object, object>>.Create( PSBinaryOperationBinder.Get(ExpressionType.Equal, ignoreCase, scalarCompare: true))), lhs.Cast(typeof(object)), rhs.Cast(typeof(object))); }
internal Expression CallAddPipe(Expression expr, Expression pipe) { if (!PSEnumerableBinder.IsStaticTypePossiblyEnumerable(expr.Type)) { return Expression.Call(pipe, CachedReflectionInfo.Pipe_Add, expr.Cast(typeof(object))); } return DynamicExpression.Dynamic(PSPipeWriterBinder.Get(), typeof(void), expr, pipe, _executionContextParameter); }
internal static Expression ConvertStringToNumber(Expression expr, Type toType) { if (!toType.IsNumeric()) { toType = typeof(int); } return Expression.Call(CachedReflectionInfo.Parser_ScanNumber, expr.Cast(typeof(string)), Expression.Constant(toType)); }
public ArgumentsWrap(bool isNewArray, Expression[] args) { _isNewArray = isNewArray; var invalid = args.FirstOrDefault(x => !(x is IEvaluatesToString)); if (invalid != null) throw new InvalidExpressionInRpcCallException(invalid, "Not supported expression"); _args = args.Cast<IEvaluatesToString>().ToArray(); }