コード例 #1
0
 internal static void InvalidateCache()
 {
     lock (PSSetIndexBinder._binderCache)
     {
         foreach (PSSetIndexBinder value in PSSetIndexBinder._binderCache.Values)
         {
             PSSetIndexBinder pSSetIndexBinder = value;
             pSSetIndexBinder._version = pSSetIndexBinder._version + 1;
         }
     }
 }
コード例 #2
0
        public static PSSetIndexBinder Get(int argCount, PSMethodInvocationConstraints constraints = null)
        {
            PSSetIndexBinder pSSetIndexBinder = null;
            PSSetIndexBinder pSSetIndexBinder1;

            lock (PSSetIndexBinder._binderCache)
            {
                Tuple <CallInfo, PSMethodInvocationConstraints> tuple = Tuple.Create <CallInfo, PSMethodInvocationConstraints>(new CallInfo(argCount, new string[0]), constraints);
                if (!PSSetIndexBinder._binderCache.TryGetValue(tuple, out pSSetIndexBinder))
                {
                    pSSetIndexBinder = new PSSetIndexBinder(tuple);
                    PSSetIndexBinder._binderCache.Add(tuple, pSSetIndexBinder);
                }
                pSSetIndexBinder1 = pSSetIndexBinder;
            }
            return(pSSetIndexBinder1);
        }
コード例 #3
0
        public Expression SetValue(Compiler compiler, Expression rhs)
        {
            Expression                    expression3;
            ParameterExpression           element = Expression.Variable(rhs.Type);
            ArrayLiteralAst               index   = this.IndexExpressionAst.Index as ArrayLiteralAst;
            PSMethodInvocationConstraints invocationConstraints = this.GetInvocationConstraints();
            Expression                    targetExpr            = this.GetTargetExpr(compiler);

            if (index != null)
            {
                expression3 = Expression.Dynamic(PSSetIndexBinder.Get(index.Elements.Count, invocationConstraints), typeof(object), index.Elements.Select <ExpressionAst, Expression>(new Func <ExpressionAst, Expression>(compiler.Compile)).Prepend <Expression>(targetExpr).Append <Expression>(element));
            }
            else
            {
                expression3 = Expression.Dynamic(PSSetIndexBinder.Get(1, invocationConstraints), typeof(object), targetExpr, this.GetIndexExpr(compiler), element);
            }
            return(Expression.Block(new ParameterExpression[] { element }, new Expression[] { Expression.Assign(element, rhs), expression3, element }));
        }
コード例 #4
0
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            string     str;
            Expression expression;
            Expression expression2;

            if (!target.HasValue || !args[0].HasValue)
            {
                return(base.Defer(target, new DynamicMetaObject[] { args[0] }).WriteToDebugLog(this));
            }
            DynamicMetaObject obj2 = args[0];
            object            obj3 = PSObject.Base(obj2.Value);

            if (obj3 is string)
            {
                str = (string)obj3;
                if (obj2.Value is PSObject)
                {
                    expression = Expression.Call(CachedReflectionInfo.PSObject_Base, obj2.Expression).Cast(typeof(string));
                }
                else
                {
                    expression = obj2.Expression.Cast(typeof(string));
                }
            }
            else
            {
                str        = PSObject.ToStringParser(null, obj2.Value);
                expression = PSToStringBinder.InvokeToString(ExpressionCache.NullConstant, obj2.Expression);
            }
            DynamicMetaObject   obj4         = PSSetMemberBinder.Get(str, this._static).FallbackSetMember(target, args[1]);
            BindingRestrictions restrictions = obj4.Restrictions.Merge(args[0].PSGetTypeRestriction()).Merge(BindingRestrictions.GetExpressionRestriction(Expression.Call(CachedReflectionInfo.String_Equals, Expression.Constant(str), expression, ExpressionCache.Ordinal)));

            if (target.Value is IDictionary)
            {
                ParameterExpression variable = Expression.Variable(typeof(Exception));
                expression2 = Expression.TryCatch(PSSetIndexBinder.Get(1, null).FallbackSetIndex(target, new DynamicMetaObject[] { args[0] }, args[1]).Expression, new CatchBlock[] { Expression.Catch(variable, Expression.Block(Expression.Call(CachedReflectionInfo.CommandProcessorBase_CheckForSevereException, variable), obj4.Expression)) });
            }
            else
            {
                expression2 = obj4.Expression;
            }
            return(new DynamicMetaObject(expression2, restrictions).WriteToDebugLog(this));
        }
コード例 #5
0
        private DynamicMetaObject SetIndexArray(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            Array arrays = (Array)target.Value;

            if (arrays.Rank <= 1)
            {
                if ((int)indexes.Length <= 1)
                {
                    Expression expression = PSGetIndexBinder.ConvertIndex(indexes[0], typeof(int));
                    if (expression != null)
                    {
                        Type       elementType = target.LimitType.GetElementType();
                        Expression expression1 = PSGetIndexBinder.ConvertIndex(value, elementType);
                        if (expression1 != null)
                        {
                            PSSetIndexBinder  pSSetIndexBinder   = this;
                            DynamicMetaObject dynamicMetaObject  = new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject1 = new DynamicMetaObject(expression, indexes[0].PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject2 = new DynamicMetaObject(expression1, value.PSGetTypeRestriction());
                            PropertyInfo      property           = target.LimitType.GetProperty("Length");
                            return(pSSetIndexBinder.IndexWithNegativeChecks(dynamicMetaObject, dynamicMetaObject1, dynamicMetaObject2, property, (Expression t, Expression i, Expression v) =>
                            {
                                Expression[] expressionArray = new Expression[1];
                                expressionArray[0] = i;
                                return Expression.Assign(Expression.ArrayAccess(t, expressionArray), v);
                            }
                                                                            ));
                        }
                        else
                        {
                            DynamicMetaObject dynamicMetaObject3 = errorSuggestion;
                            DynamicMetaObject dynamicMetaObject4 = dynamicMetaObject3;
                            if (dynamicMetaObject3 == null)
                            {
                                dynamicMetaObject4 = PSConvertBinder.ThrowNoConversion(value, elementType, this, this._version, indexes.Prepend <DynamicMetaObject>(target).OfType <DynamicMetaObject>().ToArray <DynamicMetaObject>());
                            }
                            return(dynamicMetaObject4);
                        }
                    }
                    else
                    {
                        DynamicMetaObject dynamicMetaObject5 = errorSuggestion;
                        DynamicMetaObject dynamicMetaObject6 = dynamicMetaObject5;
                        if (dynamicMetaObject5 == null)
                        {
                            DynamicMetaObject[] dynamicMetaObjectArray = new DynamicMetaObject[2];
                            dynamicMetaObjectArray[0] = target;
                            dynamicMetaObjectArray[1] = value;
                            dynamicMetaObject6        = PSConvertBinder.ThrowNoConversion(indexes[0], typeof(int), this, this._version, dynamicMetaObjectArray);
                        }
                        return(dynamicMetaObject6);
                    }
                }
                else
                {
                    DynamicMetaObject dynamicMetaObject7 = errorSuggestion;
                    DynamicMetaObject dynamicMetaObject8 = dynamicMetaObject7;
                    if (dynamicMetaObject7 == null)
                    {
                        DynamicMetaObject   dynamicMetaObject9      = target;
                        DynamicMetaObject[] dynamicMetaObjectArray1 = indexes;
                        BindingRestrictions bindingRestriction      = value.PSGetTypeRestriction();
                        string              str = "ArraySliceAssignmentFailed";
                        string              arraySliceAssignmentFailed = ParserStrings.ArraySliceAssignmentFailed;
                        Expression[]        expressionArray1           = new Expression[1];
                        Expression[]        expressionArray2           = expressionArray1;
                        int                 num = 0;
                        MethodInfo          arrayOpsIndexStringMessage = CachedReflectionInfo.ArrayOps_IndexStringMessage;
                        Type                type = typeof(object);
                        DynamicMetaObject[] dynamicMetaObjectArray2 = indexes;
                        expressionArray2[num] = Expression.Call(arrayOpsIndexStringMessage, Expression.NewArrayInit(type, dynamicMetaObjectArray2.Select <DynamicMetaObject, Expression>((DynamicMetaObject i) => i.Expression.Cast(typeof(object)))));
                        dynamicMetaObject8    = dynamicMetaObject9.ThrowRuntimeError(dynamicMetaObjectArray1, bindingRestriction, str, arraySliceAssignmentFailed, expressionArray1);
                    }
                    return(dynamicMetaObject8);
                }
            }
            else
            {
                return(this.SetIndexMultiDimensionArray(target, indexes, value, errorSuggestion));
            }
        }
コード例 #6
0
        private DynamicMetaObject InvokeIndexer(DynamicMetaObject target, DynamicMetaObject[] indexes, DynamicMetaObject value, DynamicMetaObject errorSuggestion, string methodName)
        {
            DynamicMetaObject dynamicMetaObject;
            Func <Expression, Expression, Expression, Expression> func = null;
            MethodInfo methodInfo = PSInvokeMemberBinder.FindBestMethod(target, indexes.Append <DynamicMetaObject>(value), string.Concat("set_", methodName), false, this._constraints);

            if (methodInfo != null)
            {
                ParameterInfo[] parameters = methodInfo.GetParameters();
                if ((int)parameters.Length == (int)indexes.Length + 1)
                {
                    Expression[] expressionArray = new Expression[(int)parameters.Length];
                    int          num             = 0;
                    while (num < (int)parameters.Length)
                    {
                        Type         parameterType    = parameters[num].ParameterType;
                        Expression[] expressionArray1 = expressionArray;
                        int          num1             = num;
                        if (num == (int)parameters.Length - 1)
                        {
                            dynamicMetaObject = value;
                        }
                        else
                        {
                            dynamicMetaObject = indexes[num];
                        }
                        expressionArray1[num1] = PSGetIndexBinder.ConvertIndex(dynamicMetaObject, parameterType);
                        if (expressionArray[num] != null)
                        {
                            num++;
                        }
                        else
                        {
                            DynamicMetaObject dynamicMetaObject1 = errorSuggestion;
                            DynamicMetaObject dynamicMetaObject2 = dynamicMetaObject1;
                            if (dynamicMetaObject1 == null)
                            {
                                dynamicMetaObject2 = PSConvertBinder.ThrowNoConversion(target, parameterType, this, this._version, indexes.Append <DynamicMetaObject>(value).ToArray <DynamicMetaObject>());
                            }
                            return(dynamicMetaObject2);
                        }
                    }
                    if ((int)parameters.Length == 2 && parameters[0].ParameterType.Equals(typeof(int)) && target.Value as IDictionary == null)
                    {
                        PropertyInfo property     = target.LimitType.GetProperty("Length");
                        PropertyInfo propertyInfo = property;
                        if (property == null)
                        {
                            propertyInfo = target.LimitType.GetProperty("Count");
                        }
                        PropertyInfo propertyInfo1 = propertyInfo;
                        if (propertyInfo1 != null)
                        {
                            PSSetIndexBinder  pSSetIndexBinder   = this;
                            DynamicMetaObject dynamicMetaObject3 = new DynamicMetaObject(target.Expression.Cast(target.LimitType), target.PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject4 = new DynamicMetaObject(expressionArray[0], indexes[0].PSGetTypeRestriction());
                            DynamicMetaObject dynamicMetaObject5 = new DynamicMetaObject(expressionArray[1], value.PSGetTypeRestriction());
                            PropertyInfo      propertyInfo2      = propertyInfo1;
                            if (func == null)
                            {
                                func = (Expression t, Expression i, Expression v) => Expression.Call(t, methodInfo, i, v);
                            }
                            return(pSSetIndexBinder.IndexWithNegativeChecks(dynamicMetaObject3, dynamicMetaObject4, dynamicMetaObject5, propertyInfo2, func));
                        }
                    }
                    BindingRestrictions bindingRestriction = target.CombineRestrictions(indexes).Merge(value.PSGetTypeRestriction());
                    bindingRestriction = bindingRestriction.Merge(BinderUtils.GetVersionCheck(this, this._version));
                    bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage());
                    Expression          expression          = expressionArray[(int)expressionArray.Length - 1];
                    ParameterExpression parameterExpression = Expression.Parameter(expression.Type, "value");
                    expressionArray[(int)expressionArray.Length - 1] = parameterExpression;
                    ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
                    parameterExpressionArray[0] = parameterExpression;
                    Expression[] expressionArray2 = new Expression[3];
                    expressionArray2[0] = Expression.Assign(parameterExpression, expression);
                    expressionArray2[1] = Expression.Call(target.Expression.Cast(target.LimitType), methodInfo, expressionArray);
                    expressionArray2[2] = parameterExpression.Cast(typeof(object));
                    return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray2), bindingRestriction));
                }
                else
                {
                    DynamicMetaObject dynamicMetaObject6 = errorSuggestion;
                    DynamicMetaObject dynamicMetaObject7 = dynamicMetaObject6;
                    if (dynamicMetaObject6 == null)
                    {
                        dynamicMetaObject7 = this.CannotIndexTarget(target, indexes, value);
                    }
                    return(dynamicMetaObject7);
                }
            }
            else
            {
                DynamicMetaObject dynamicMetaObject8 = errorSuggestion;
                DynamicMetaObject dynamicMetaObject9 = dynamicMetaObject8;
                if (dynamicMetaObject8 == null)
                {
                    dynamicMetaObject9 = this.CannotIndexTarget(target, indexes, value);
                }
                return(dynamicMetaObject9);
            }
        }
コード例 #7
0
ファイル: PSSetIndexBinder.cs プロジェクト: nickchal/pash
 public static PSSetIndexBinder Get(int argCount, PSMethodInvocationConstraints constraints = null)
 {
     PSSetIndexBinder pSSetIndexBinder = null;
     PSSetIndexBinder pSSetIndexBinder1;
     lock (PSSetIndexBinder._binderCache)
     {
         Tuple<CallInfo, PSMethodInvocationConstraints> tuple = Tuple.Create<CallInfo, PSMethodInvocationConstraints>(new CallInfo(argCount, new string[0]), constraints);
         if (!PSSetIndexBinder._binderCache.TryGetValue(tuple, out pSSetIndexBinder))
         {
             pSSetIndexBinder = new PSSetIndexBinder(tuple);
             PSSetIndexBinder._binderCache.Add(tuple, pSSetIndexBinder);
         }
         pSSetIndexBinder1 = pSSetIndexBinder;
     }
     return pSSetIndexBinder1;
 }