Exemplo n.º 1
0
        private DynamicMetaObject IndexWithNegativeChecks(DynamicMetaObject target, DynamicMetaObject index, DynamicMetaObject value, PropertyInfo lengthProperty, Func <Expression, Expression, Expression, Expression> generateIndexOperation)
        {
            DynamicMetaObject[] dynamicMetaObjectArray = new DynamicMetaObject[1];
            dynamicMetaObjectArray[0] = index;
            BindingRestrictions bindingRestriction = target.CombineRestrictions(dynamicMetaObjectArray).Merge(value.Restrictions);

            bindingRestriction = bindingRestriction.Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, target.LimitType, this._version));
            ParameterExpression parameterExpression  = Expression.Parameter(target.LimitType, "target");
            ParameterExpression parameterExpression1 = Expression.Parameter(typeof(int), "len");
            Expression          expression           = value.Expression;
            ParameterExpression parameterExpression2 = Expression.Parameter(expression.Type, "value");
            ParameterExpression parameterExpression3 = Expression.Parameter(typeof(int), "index");

            ParameterExpression[] parameterExpressionArray = new ParameterExpression[4];
            parameterExpressionArray[0] = parameterExpression;
            parameterExpressionArray[1] = parameterExpression2;
            parameterExpressionArray[2] = parameterExpression1;
            parameterExpressionArray[3] = parameterExpression3;
            Expression[] expressionArray = new Expression[7];
            expressionArray[0] = Expression.Assign(parameterExpression, target.Expression.Cast(target.LimitType));
            expressionArray[1] = Expression.Assign(parameterExpression2, expression);
            expressionArray[2] = Expression.Assign(parameterExpression1, Expression.Property(parameterExpression, lengthProperty));
            expressionArray[3] = Expression.Assign(parameterExpression3, index.Expression);
            expressionArray[4] = Expression.IfThen(Expression.LessThan(parameterExpression3, ExpressionCache.Constant(0)), Expression.Assign(parameterExpression3, Expression.Add(parameterExpression3, parameterExpression1)));
            expressionArray[5] = generateIndexOperation(parameterExpression, parameterExpression3, parameterExpression2);
            expressionArray[6] = parameterExpression2.Cast(typeof(object));
            return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray), bindingRestriction));
        }
Exemplo n.º 2
0
        public override DynamicMetaObject FallbackConvert(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
        {
            bool flag;

            if (!target.HasValue)
            {
                return(base.Defer(target, new DynamicMetaObject[0]).WriteToDebugLog(this));
            }
            if (target.Value == AutomationNull.Value)
            {
                return(new DynamicMetaObject(Expression.Default(base.Type), target.PSGetTypeRestriction()).WriteToDebugLog(this));
            }
            Type resultType = base.Type;

            LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(target.Value, resultType, out flag);
            if ((errorSuggestion != null) && (target.Value is DynamicObject))
            {
                return(errorSuggestion.WriteToDebugLog(this));
            }
            BindingRestrictions restrictions = target.PSGetTypeRestriction().Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, resultType, this._version));

            return(new DynamicMetaObject(InvokeConverter(conversion, target.Expression, resultType, flag, ExpressionCache.InvariantCulture), restrictions).WriteToDebugLog(this));
        }
Exemplo n.º 3
0
        internal static DynamicMetaObject ThrowNoConversion(DynamicMetaObject target, Type toType, DynamicMetaObjectBinder binder, int currentVersion, params DynamicMetaObject[] args)
        {
            Expression expression = Expression.Call(CachedReflectionInfo.LanguagePrimitives_ThrowInvalidCastException, target.Expression.Cast(typeof(object)), Expression.Constant(toType));

            if (!binder.ReturnType.Equals(typeof(void)))
            {
                expression = Expression.Block(expression, Expression.Default(binder.ReturnType));
            }
            return(new DynamicMetaObject(expression, target.CombineRestrictions(args).Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(binder, toType, currentVersion))));
        }
Exemplo n.º 4
0
        private DynamicMetaObject GetIndexDictionary(DynamicMetaObject target, DynamicMetaObject[] indexes, Type idictionary)
        {
            bool flag;

            if (indexes.Length > 1)
            {
                return(null);
            }
            MethodInfo method = idictionary.GetMethod("TryGetValue");

            ParameterInfo[] parameters    = method.GetParameters();
            Type            parameterType = parameters[0].ParameterType;

            LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(indexes[0].Value, parameterType, out flag);
            if (conversion.Rank == ConversionRank.None)
            {
                return(null);
            }
            if (indexes[0].LimitType.IsArray && !parameterType.IsArray)
            {
                return(null);
            }
            BindingRestrictions restrictions = target.CombineRestrictions(indexes).Merge(BinderUtils.GetOptionalVersionAndLanguageCheckForType(this, parameterType, this._version));
            Expression          expression   = PSConvertBinder.InvokeConverter(conversion, indexes[0].Expression, parameterType, flag, ExpressionCache.InvariantCulture);
            ParameterExpression expression2  = Expression.Parameter(parameters[1].ParameterType.GetElementType(), "outParam");

            return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression2 }, new Expression[] { Expression.Condition(Expression.Call(target.Expression.Cast(idictionary), method, expression, expression2), expression2.Cast(typeof(object)), this.GetNullResult()) }), restrictions));
        }