Пример #1
0
        internal static PSMemberInfo ResolveAlias(PSAliasProperty alias, DynamicMetaObject target, HashSet <string> aliases, List <BindingRestrictions> aliasRestrictions)
        {
            bool flag;
            Type type;
            BindingRestrictions restrictions;

            if (aliases == null)
            {
                aliases = new HashSet <string> {
                    alias.Name
                };
            }
            else
            {
                if (aliases.Contains(alias.Name))
                {
                    throw new ExtendedTypeSystemException("CycleInAliasLookup", null, ExtendedTypeSystem.CycleInAlias, new object[] { alias.Name });
                }
                aliases.Add(alias.Name);
            }
            PSGetMemberBinder binder = Get(alias.ReferencedMemberName, false);

            if (binder.HasInstanceMember)
            {
                return(null);
            }
            PSMemberInfo info = binder.GetPSMemberInfo(target, out restrictions, out flag, out type, aliases, aliasRestrictions);

            aliasRestrictions.Add(BinderUtils.GetVersionCheck(binder, binder._version));
            return(info);
        }
Пример #2
0
 private PSInvokeMemberBinder(string name, bool ignoreCase, bool @static, bool propertySetter, bool nonEnumerating, CallInfo callInfo, PSMethodInvocationConstraints invocationConstraints)
     : base(name, ignoreCase, callInfo)
 {
     this._static                = @static;
     this._propertySetter        = propertySetter;
     this._nonEnumerating        = nonEnumerating;
     this._invocationConstraints = invocationConstraints;
     this._getMemberBinder       = PSGetMemberBinder.Get(name, @static);
 }
Пример #3
0
        internal static object SetAdaptedValue(object obj, string member, object value)
        {
            object obj2;

            try
            {
                ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                PSMemberInfo     memberInfo = null;
                if ((executionContextFromTLS != null) && (executionContextFromTLS.TypeTable != null))
                {
                    ConsolidatedString typeNames = PSObject.GetTypeNames(obj);
                    memberInfo = executionContextFromTLS.TypeTable.GetMembers <PSMemberInfo>(typeNames)[member];
                    if (memberInfo != null)
                    {
                        memberInfo = PSGetMemberBinder.CloneMemberInfo(memberInfo, obj);
                    }
                }
                PSObject.AdapterSet mappedAdapter = PSObject.GetMappedAdapter(obj, (executionContextFromTLS != null) ? executionContextFromTLS.TypeTable : null);
                if (memberInfo == null)
                {
                    memberInfo = mappedAdapter.OriginalAdapter.BaseGetMember <PSMemberInfo>(obj, member);
                }
                if ((memberInfo == null) && (mappedAdapter.DotNetAdapter != null))
                {
                    memberInfo = mappedAdapter.DotNetAdapter.BaseGetMember <PSMemberInfo>(obj, member);
                }
                if (memberInfo == null)
                {
                    throw InterpreterError.NewInterpreterException(null, typeof(RuntimeException), null, "PropertyAssignmentException", ParserStrings.PropertyNotFound, new object[] { member });
                }
                memberInfo.Value = value;
                obj2             = value;
            }
            catch (SetValueException)
            {
                throw;
            }
            catch (Exception exception)
            {
                ExceptionHandlingOps.ConvertToMethodInvocationException(exception, typeof(SetValueInvocationException), member, 0, null);
                throw;
            }
            return(obj2);
        }
Пример #4
0
 internal static void InvalidateCache()
 {
     lock (PSInvokeMemberBinder._binderCache)
     {
         foreach (PSInvokeMemberBinder value in PSInvokeMemberBinder._binderCache.Values)
         {
             PSGetMemberBinder pSGetMemberBinder = value._getMemberBinder;
             pSGetMemberBinder._version = pSGetMemberBinder._version + 1;
         }
     }
     lock (PSInvokeMemberBinder._staticBinderCache)
     {
         foreach (PSInvokeMemberBinder pSInvokeMemberBinder in PSInvokeMemberBinder._staticBinderCache.Values)
         {
             PSGetMemberBinder pSGetMemberBinder1 = pSInvokeMemberBinder._getMemberBinder;
             pSGetMemberBinder1._version = pSGetMemberBinder1._version + 1;
         }
     }
 }
Пример #5
0
        public Expression GetValue(Compiler compiler, List <Expression> exprs, List <ParameterExpression> temps)
        {
            Expression          targetExpr = this.GetTargetExpr(compiler);
            ParameterExpression item       = Expression.Parameter(targetExpr.Type);

            temps.Add(item);
            this.CachedTarget = item;
            exprs.Add(Expression.Assign(item, targetExpr));
            StringConstantExpressionAst member = this.MemberExpression.Member as StringConstantExpressionAst;

            if (member != null)
            {
                return(Expression.Dynamic(PSGetMemberBinder.Get(member.Value, this.MemberExpression.Static), typeof(object), item));
            }
            ParameterExpression expression4 = Expression.Parameter(this.GetPropertyExpr(compiler).Type);

            temps.Add(expression4);
            exprs.Add(Expression.Assign(expression4, compiler.Compile(this.MemberExpression.Member)));
            this.CachedPropertyExpr = expression4;
            return(Expression.Dynamic(PSGetDynamicMemberBinder.Get(this.MemberExpression.Static), typeof(object), item, expression4));
        }
        public override DynamicMetaObject Bind(DynamicMetaObject target, DynamicMetaObject[] args)
        {
            string              str;
            Expression          expression;
            BindingRestrictions restrictions;

            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
            {
                if (target.Value is IDictionary)
                {
                    restrictions = target.PSGetTypeRestriction().Merge(BindingRestrictions.GetExpressionRestriction(Expression.Not(Expression.TypeIs(args[0].Expression, typeof(string)))));
                    return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.PSGetDynamicMemberBinder_GetIDictionaryMember, target.Expression.Cast(typeof(IDictionary)), args[0].Expression.Cast(typeof(object))), restrictions).WriteToDebugLog(this));
                }
                str        = PSObject.ToStringParser(null, obj3);
                expression = PSToStringBinder.InvokeToString(ExpressionCache.NullConstant, obj2.Expression);
            }
            DynamicMetaObject obj4 = PSGetMemberBinder.Get(str, this._static).FallbackGetMember(target);

            restrictions = obj4.Restrictions.Merge(args[0].PSGetTypeRestriction()).Merge(BindingRestrictions.GetExpressionRestriction(Expression.Call(CachedReflectionInfo.String_Equals, Expression.Constant(str), expression, ExpressionCache.Ordinal)));
            return(new DynamicMetaObject(obj4.Expression, restrictions).WriteToDebugLog(this));
        }
Пример #7
0
            public override DynamicMetaObject FallbackGetMember(DynamicMetaObject target, DynamicMetaObject errorSuggestion)
            {
                MethodInfo method     = null;
                Expression expression = null;
                string     name       = base.Name;

                if (name != null)
                {
                    if (!(name == "psadapted"))
                    {
                        if (name == "psbase")
                        {
                            method     = CachedReflectionInfo.ReservedNameMembers_GeneratePSBaseMemberSet;
                            expression = target.Expression.Cast(typeof(object));
                        }
                        else if (name == "psextended")
                        {
                            method     = CachedReflectionInfo.ReservedNameMembers_GeneratePSExtendedMemberSet;
                            expression = target.Expression.Cast(typeof(object));
                        }
                        else if (name == "psobject")
                        {
                            method     = CachedReflectionInfo.ReservedNameMembers_GeneratePSObjectMemberSet;
                            expression = target.Expression.Cast(typeof(object));
                        }
                        else if (name == "pstypenames")
                        {
                            method     = CachedReflectionInfo.ReservedNameMembers_PSTypeNames;
                            expression = target.Expression.Convert(typeof(PSObject));
                        }
                    }
                    else
                    {
                        method     = CachedReflectionInfo.ReservedNameMembers_GeneratePSAdaptedMemberSet;
                        expression = target.Expression.Cast(typeof(object));
                    }
                }
                return(new DynamicMetaObject(PSGetMemberBinder.WrapGetMemberInTry(Expression.Call(method, expression)), target.PSGetTypeRestriction()));
            }
Пример #8
0
        private static PSGetMemberBinder Get(string memberName, bool @static, bool nonEnumerating)
        {
            PSGetMemberBinder binder;
            Dictionary <string, PSGetMemberBinder> dictionary = @static ? _staticBinderCache : (nonEnumerating ? _binderCacheNonEnumerable : _binderCache);

            lock (dictionary)
            {
                if (dictionary.TryGetValue(memberName, out binder))
                {
                    return(binder);
                }
                if (PSMemberInfoCollection <PSMemberInfo> .IsReservedName(memberName))
                {
                    binder = dictionary[memberName.ToLowerInvariant()];
                }
                else
                {
                    binder = new PSGetMemberBinder(memberName, true, @static, nonEnumerating);
                    if (!@static)
                    {
                        List <PSGetMemberBinder> orAdd = _binderCacheIgnoringCase.GetOrAdd(memberName, _ => new List <PSGetMemberBinder>());
                        lock (orAdd)
                        {
                            if (orAdd.Any <PSGetMemberBinder>())
                            {
                                binder._hasInstanceMember  = orAdd[0]._hasInstanceMember;
                                binder._hasTypeTableMember = orAdd[0]._hasTypeTableMember;
                            }
                            orAdd.Add(binder);
                        }
                    }
                }
                dictionary.Add(memberName, binder);
            }
            return(binder);
        }
Пример #9
0
        public override DynamicMetaObject FallbackSetMember(DynamicMetaObject target, DynamicMetaObject value, DynamicMetaObject errorSuggestion)
        {
            PSMemberInfo        info;
            BindingRestrictions restrictions2;
            bool flag3;
            Type type4;

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

            if (obj2 == null)
            {
                return(target.ThrowRuntimeError(new DynamicMetaObject[] { value }, BindingRestrictions.Empty, "PropertyNotFound", ParserStrings.PropertyNotFound, new Expression[] { Expression.Constant(base.Name) }).WriteToDebugLog(this));
            }
            if (value.Value == AutomationNull.Value)
            {
                value = new DynamicMetaObject(ExpressionCache.NullConstant, value.PSGetTypeRestriction(), null);
            }
            if (this._getMemberBinder.HasInstanceMember && PSGetMemberBinder.TryGetInstanceMember(target.Value, base.Name, out info))
            {
                ParameterExpression   expression   = Expression.Variable(typeof(PSMemberInfo));
                ParameterExpression   expression2  = Expression.Variable(typeof(object));
                ConditionalExpression expression3  = Expression.Condition(Expression.Call(CachedReflectionInfo.PSGetMemberBinder_TryGetInstanceMember, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), expression), Expression.Assign(Expression.Property(expression, "Value"), value.Expression.Cast(typeof(object))), base.GetUpdateExpression(typeof(object)));
                BindingRestrictions   restrictions = BinderUtils.GetVersionCheck(this._getMemberBinder, this._getMemberBinder._version).Merge(value.PSGetTypeRestriction());
                return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression, expression2 }, new Expression[] { expression3 }), restrictions).WriteToDebugLog(this));
            }
            if (obj2 is IDictionary)
            {
                Type genericTypeArg = null;
                bool flag           = PSGetMemberBinder.IsGenericDictionary(obj2, ref genericTypeArg);
                if (!flag || (genericTypeArg != null))
                {
                    bool                flag2;
                    Type                type       = flag ? typeof(IDictionary <,>).MakeGenericType(new Type[] { typeof(string), genericTypeArg }) : typeof(IDictionary);
                    MethodInfo          method     = type.GetMethod("set_Item");
                    ParameterExpression left       = Expression.Variable(genericTypeArg ?? typeof(object));
                    Type                resultType = left.Type;
                    LanguagePrimitives.ConversionData conversion = LanguagePrimitives.FigureConversion(value.Value, resultType, out flag2);
                    if (conversion.Rank != ConversionRank.None)
                    {
                        Expression right = PSConvertBinder.InvokeConverter(conversion, value.Expression, resultType, flag2, ExpressionCache.InvariantCulture);
                        return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { left }, new Expression[] { Expression.Assign(left, right), Expression.Call(PSGetMemberBinder.GetTargetExpr(target).Cast(type), method, Expression.Constant(base.Name), right), right.Cast(typeof(object)) }), target.CombineRestrictions(new DynamicMetaObject[] { value })).WriteToDebugLog(this));
                    }
                }
            }
            info          = this._getMemberBinder.GetPSMemberInfo(target, out restrictions2, out flag3, out type4, null, null);
            restrictions2 = restrictions2.Merge(value.PSGetTypeRestriction());
            if (ExecutionContext.HasEverUsedConstrainedLanguage)
            {
                restrictions2 = restrictions2.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage());
                DynamicMetaObject obj3 = PSGetMemberBinder.EnsureAllowedInLanguageMode(LocalPipeline.GetExecutionContextFromTLS().LanguageMode, target, obj2, base.Name, this._static, new DynamicMetaObject[] { value }, restrictions2, "PropertySetterNotSupportedInConstrainedLanguage", ParserStrings.PropertySetConstrainedLanguage);
                if (obj3 != null)
                {
                    return(obj3.WriteToDebugLog(this));
                }
            }
            if (flag3)
            {
                if (info == null)
                {
                    return((errorSuggestion ?? new DynamicMetaObject(Compiler.ThrowRuntimeError("PropertyAssignmentException", ParserStrings.PropertyNotFound, this.ReturnType, new Expression[] { Expression.Constant(base.Name) }), restrictions2)).WriteToDebugLog(this));
                }
                PSPropertyInfo info3 = info as PSPropertyInfo;
                if (info3 != null)
                {
                    if (!info3.IsSettable)
                    {
                        Expression innerException = Expression.New(CachedReflectionInfo.SetValueException_ctor, new Expression[] { Expression.Constant("PropertyAssignmentException"), Expression.Constant(null, typeof(Exception)), Expression.Constant(ParserStrings.PropertyIsReadOnly), Expression.NewArrayInit(typeof(object), new Expression[] { Expression.Constant(base.Name) }) });
                        return(new DynamicMetaObject(Compiler.ThrowRuntimeErrorWithInnerException("PropertyAssignmentException", Expression.Constant(ParserStrings.PropertyIsReadOnly), innerException, this.ReturnType, new Expression[] { Expression.Constant(base.Name) }), restrictions2).WriteToDebugLog(this));
                    }
                    PSProperty property = info3 as PSProperty;
                    if (property != null)
                    {
                        DotNetAdapter.PropertyCacheEntry adapterData = property.adapterData as DotNetAdapter.PropertyCacheEntry;
                        if (adapterData != null)
                        {
                            Expression expression10;
                            Type       propertyType;
                            if (adapterData.member.DeclaringType.IsGenericTypeDefinition)
                            {
                                Expression expression9 = Expression.New(CachedReflectionInfo.SetValueException_ctor, new Expression[] { Expression.Constant("PropertyAssignmentException"), Expression.Constant(null, typeof(Exception)), Expression.Constant(ExtendedTypeSystem.CannotInvokeStaticMethodOnUninstantiatedGenericType), Expression.NewArrayInit(typeof(object), new Expression[] { Expression.Constant(adapterData.member.DeclaringType.FullName) }) });
                                return(new DynamicMetaObject(Compiler.ThrowRuntimeErrorWithInnerException("PropertyAssignmentException", Expression.Constant(ExtendedTypeSystem.CannotInvokeStaticMethodOnUninstantiatedGenericType), expression9, this.ReturnType, new Expression[] { Expression.Constant(adapterData.member.DeclaringType.FullName) }), restrictions2).WriteToDebugLog(this));
                            }
                            PropertyInfo member = adapterData.member as PropertyInfo;
                            if (member != null)
                            {
                                propertyType = member.PropertyType;
                                Expression expression11 = this._static ? null : PSGetMemberBinder.GetTargetExpr(target);
                                expression10 = Expression.Property(expression11, member);
                            }
                            else
                            {
                                FieldInfo field = (FieldInfo)adapterData.member;
                                propertyType = field.FieldType;
                                Expression expression12 = this._static ? null : PSGetMemberBinder.GetTargetExpr(target);
                                expression10 = Expression.Field(expression12, field);
                            }
                            Type underlyingType = Nullable.GetUnderlyingType(propertyType);
                            Type type7          = underlyingType ?? propertyType;
                            ParameterExpression expression13 = Expression.Variable(type7);
                            Expression          expression14 = (underlyingType != null) ? ((value.Value == null) ? ((Expression)Expression.Constant(null, propertyType)) : ((Expression)Expression.New(propertyType.GetConstructor(new Type[] { underlyingType }), new Expression[] { expression13 }))) : ((Expression)expression13);
                            Expression          expression15 = (type7.Equals(typeof(object)) && value.LimitType.Equals(typeof(PSObject))) ? Expression.Call(CachedReflectionInfo.PSObject_Base, value.Expression.Cast(typeof(PSObject))) : value.CastOrConvert(type7);
                            Expression          expr         = Expression.Block(new ParameterExpression[] { expression13 }, new Expression[] { Expression.Assign(expression13, expression15), Expression.Assign(expression10, expression14), expression13.Cast(typeof(object)) });
                            ParameterExpression variable     = Expression.Variable(typeof(Exception));
                            return(new DynamicMetaObject(Expression.TryCatch(expr.Cast(typeof(object)), new CatchBlock[] { Expression.Catch(variable, Expression.Block(Expression.Call(CachedReflectionInfo.ExceptionHandlingOps_ConvertToMethodInvocationException, variable, Expression.Constant(typeof(SetValueInvocationException)), Expression.Constant(base.Name), ExpressionCache.Constant(0), Expression.Constant(null, typeof(MemberInfo))), Expression.Rethrow(typeof(object)))) }), restrictions2).WriteToDebugLog(this));
                        }
                    }
                    PSCodeProperty property2 = info3 as PSCodeProperty;
                    if (property2 != null)
                    {
                        ParameterExpression expression17 = Expression.Variable(typeof(object));
                        return(new DynamicMetaObject(Expression.Block(new ParameterExpression[] { expression17 }, new Expression[] { Expression.Assign(expression17, value.CastOrConvert(expression17.Type)), PSInvokeMemberBinder.InvokeMethod(property2.SetterCodeReference, null, new DynamicMetaObject[] { target, value }, false), expression17 }), restrictions2).WriteToDebugLog(this));
                    }
                    PSScriptProperty property3 = info3 as PSScriptProperty;
                    if (property3 != null)
                    {
                        return(new DynamicMetaObject(Expression.Call(Expression.Constant(property3), CachedReflectionInfo.PSScriptProperty_InvokeSetter, PSGetMemberBinder.GetTargetExpr(target), value.Expression.Cast(typeof(object))), restrictions2).WriteToDebugLog(this));
                    }
                }
                if (errorSuggestion != null)
                {
                    return(errorSuggestion.WriteToDebugLog(this));
                }
            }
            return(new DynamicMetaObject(Expression.Call(CachedReflectionInfo.PSSetMemberBinder_SetAdaptedValue, PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object)), Expression.Constant(base.Name), value.Expression.Cast(typeof(object))), restrictions2).WriteToDebugLog(this));
        }
Пример #10
0
 public PSSetMemberBinder(string name, bool ignoreCase, bool @static) : base(name, ignoreCase)
 {
     this._static          = @static;
     this._getMemberBinder = PSGetMemberBinder.Get(name, @static);
 }
Пример #11
0
        public override DynamicMetaObject FallbackInvokeMember(DynamicMetaObject target, DynamicMetaObject[] args, DynamicMetaObject errorSuggestion)
        {
            PSMemberInfo        pSMemberInfo       = null;
            BindingRestrictions bindingRestriction = null;
            bool       flag = false;
            Type       type = null;
            Expression expression;
            Type       value;

            MethodInformation[] methodInformationArray;
            Type type1;

            if (target.HasValue)
            {
                DynamicMetaObject[] dynamicMetaObjectArray = args;
                if (!dynamicMetaObjectArray.Where <DynamicMetaObject>((DynamicMetaObject arg) => !arg.HasValue).Any <DynamicMetaObject>())
                {
                    object obj = PSObject.Base(target.Value);
                    if (obj != null)
                    {
                        if (!this._getMemberBinder.HasInstanceMember || !PSGetMemberBinder.TryGetInstanceMember(target.Value, base.Name, out pSMemberInfo))
                        {
                            PSMethodInfo        pSMethodInfo            = this._getMemberBinder.GetPSMemberInfo(target, out bindingRestriction, out flag, out type, null, null) as PSMethodInfo;
                            DynamicMetaObject[] dynamicMetaObjectArray1 = args;
                            BindingRestrictions bindingRestriction1     = bindingRestriction;
                            bindingRestriction = dynamicMetaObjectArray1.Aggregate <DynamicMetaObject, BindingRestrictions>(bindingRestriction1, (BindingRestrictions current, DynamicMetaObject arg) => current.Merge(arg.PSGetMethodArgumentRestriction()));
                            if (ExecutionContext.HasEverUsedConstrainedLanguage)
                            {
                                bindingRestriction = bindingRestriction.Merge(BinderUtils.GetLanguageModeCheckIfHasEverUsedConstrainedLanguage());
                                ExecutionContext  executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                                DynamicMetaObject dynamicMetaObject       = PSGetMemberBinder.EnsureAllowedInLanguageMode(executionContextFromTLS.LanguageMode, target, obj, base.Name, this._static, args, bindingRestriction, "MethodInvocationNotSupportedInConstrainedLanguage", ParserStrings.InvokeMethodConstrainedLanguage);
                                if (dynamicMetaObject != null)
                                {
                                    return(dynamicMetaObject.WriteToDebugLog(this));
                                }
                            }
                            if (flag)
                            {
                                PSObject pSObject = target.Value as PSObject;
                                if (pSObject != null && (obj.GetType().Equals(typeof(Hashtable)) || obj.GetType().Equals(typeof(ArrayList))))
                                {
                                    if (!pSObject.isDeserialized)
                                    {
                                        bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Not(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized))));
                                    }
                                    else
                                    {
                                        pSMethodInfo       = null;
                                        bindingRestriction = bindingRestriction.Merge(BindingRestrictions.GetExpressionRestriction(Expression.Field(target.Expression.Cast(typeof(PSObject)), CachedReflectionInfo.PSObject_isDeserialized)));
                                    }
                                }
                                PSMethod pSMethod = pSMethodInfo as PSMethod;
                                if (pSMethod == null)
                                {
                                    PSScriptMethod pSScriptMethod = pSMethodInfo as PSScriptMethod;
                                    if (pSScriptMethod == null)
                                    {
                                        PSCodeMethod pSCodeMethod = pSMethodInfo as PSCodeMethod;
                                        if (pSCodeMethod == null)
                                        {
                                            PSParameterizedProperty pSParameterizedProperty = pSMethodInfo as PSParameterizedProperty;
                                            if (pSParameterizedProperty == null)
                                            {
                                                if (errorSuggestion == null)
                                                {
                                                    if (this._static)
                                                    {
                                                        value = (Type)target.Value;
                                                    }
                                                    else
                                                    {
                                                        value = PSObject.Base(target.Value).GetType();
                                                    }
                                                    Type type2 = value;
                                                    if (!this._static && !this._nonEnumerating && target.Value != AutomationNull.Value)
                                                    {
                                                        DynamicMetaObject dynamicMetaObject1 = PSEnumerableBinder.IsEnumerable(target);
                                                        if (dynamicMetaObject1 != null)
                                                        {
                                                            return(this.InvokeMemberOnCollection(dynamicMetaObject1, args, type2, bindingRestriction));
                                                        }
                                                    }
                                                    Expression[] expressionArray = new Expression[2];
                                                    expressionArray[0] = Expression.Constant(type2.FullName);
                                                    expressionArray[1] = Expression.Constant(base.Name);
                                                    return(new DynamicMetaObject(Compiler.ThrowRuntimeError("MethodNotFound", ParserStrings.MethodNotFound, expressionArray), bindingRestriction).WriteToDebugLog(this));
                                                }
                                                else
                                                {
                                                    return(errorSuggestion.WriteToDebugLog(this));
                                                }
                                            }
                                            else
                                            {
                                                DotNetAdapter.ParameterizedPropertyCacheEntry parameterizedPropertyCacheEntry = (DotNetAdapter.ParameterizedPropertyCacheEntry)pSParameterizedProperty.adapterData;
                                                PSInvokeMemberBinder pSInvokeMemberBinder    = this;
                                                DynamicMetaObject    dynamicMetaObject2      = target;
                                                DynamicMetaObject[]  dynamicMetaObjectArray2 = args;
                                                BindingRestrictions  bindingRestriction2     = bindingRestriction;
                                                if (this._propertySetter)
                                                {
                                                    methodInformationArray = parameterizedPropertyCacheEntry.setterInformation;
                                                }
                                                else
                                                {
                                                    methodInformationArray = parameterizedPropertyCacheEntry.getterInformation;
                                                }
                                                if (this._propertySetter)
                                                {
                                                    type1 = typeof(SetValueInvocationException);
                                                }
                                                else
                                                {
                                                    type1 = typeof(GetValueInvocationException);
                                                }
                                                return(pSInvokeMemberBinder.InvokeDotNetMethod(dynamicMetaObject2, dynamicMetaObjectArray2, bindingRestriction2, methodInformationArray, type1).WriteToDebugLog(this));
                                            }
                                        }
                                        else
                                        {
                                            return(new DynamicMetaObject(PSInvokeMemberBinder.InvokeMethod(pSCodeMethod.CodeReference, null, args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>(), false).Cast(typeof(object)), bindingRestriction).WriteToDebugLog(this));
                                        }
                                    }
                                    else
                                    {
                                        MethodInfo         pSScriptMethodInvokeScript = CachedReflectionInfo.PSScriptMethod_InvokeScript;
                                        ConstantExpression constantExpression         = Expression.Constant(base.Name);
                                        ConstantExpression constantExpression1        = Expression.Constant(pSScriptMethod.Script);
                                        Expression         expression1 = target.Expression.Cast(typeof(object));
                                        Type type3 = typeof(object);
                                        DynamicMetaObject[] dynamicMetaObjectArray3 = args;
                                        return(new DynamicMetaObject(Expression.Call(pSScriptMethodInvokeScript, constantExpression, constantExpression1, expression1, Expression.NewArrayInit(type3, dynamicMetaObjectArray3.Select <DynamicMetaObject, Expression>((DynamicMetaObject e) => e.Expression.Cast(typeof(object))))), bindingRestriction).WriteToDebugLog(this));
                                    }
                                }
                                else
                                {
                                    DotNetAdapter.MethodCacheEntry methodCacheEntry = (DotNetAdapter.MethodCacheEntry)pSMethod.adapterData;
                                    return(this.InvokeDotNetMethod(target, args, bindingRestriction, methodCacheEntry.methodInformationStructures, typeof(MethodException)).WriteToDebugLog(this));
                                }
                            }
                            else
                            {
                                if (!this._propertySetter)
                                {
                                    MethodInfo         pSInvokeMemberBinderInvokeAdaptedMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedMember;
                                    Expression         expression2         = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object));
                                    ConstantExpression constantExpression2 = Expression.Constant(base.Name);
                                    Type type4 = typeof(object);
                                    DynamicMetaObject[] dynamicMetaObjectArray4 = args;
                                    expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedMember, expression2, constantExpression2, Expression.NewArrayInit(type4, dynamicMetaObjectArray4.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object)))));
                                }
                                else
                                {
                                    MethodInfo         pSInvokeMemberBinderInvokeAdaptedSetMember = CachedReflectionInfo.PSInvokeMemberBinder_InvokeAdaptedSetMember;
                                    Expression         expression3         = PSGetMemberBinder.GetTargetExpr(target).Cast(typeof(object));
                                    ConstantExpression constantExpression3 = Expression.Constant(base.Name);
                                    Type type5 = typeof(object);
                                    IEnumerable <DynamicMetaObject> dynamicMetaObjects = args.Take <DynamicMetaObject>((int)args.Length - 1);
                                    expression = Expression.Call(pSInvokeMemberBinderInvokeAdaptedSetMember, expression3, constantExpression3, Expression.NewArrayInit(type5, dynamicMetaObjects.Select <DynamicMetaObject, Expression>((DynamicMetaObject arg) => arg.Expression.Cast(typeof(object)))), args.Last <DynamicMetaObject>().Expression.Cast(typeof(object)));
                                }
                                return(new DynamicMetaObject(expression, bindingRestriction).WriteToDebugLog(this));
                            }
                        }
                        else
                        {
                            ParameterExpression  parameterExpression  = Expression.Variable(typeof(PSMethodInfo));
                            MethodCallExpression methodCallExpression = Expression.Call(CachedReflectionInfo.PSInvokeMemberBinder_TryGetInstanceMethod, target.Expression.Cast(typeof(object)), Expression.Constant(base.Name), parameterExpression);
                            ParameterExpression  parameterExpression1 = parameterExpression;
                            MethodInfo           pSMethodInfoInvoke   = CachedReflectionInfo.PSMethodInfo_Invoke;
                            Expression[]         expressionArray1     = new Expression[1];
                            Expression[]         expressionArray2     = expressionArray1;
                            int  num   = 0;
                            Type type6 = typeof(object);
                            DynamicMetaObject[] dynamicMetaObjectArray5 = args;
                            expressionArray2[num] = Expression.NewArrayInit(type6, dynamicMetaObjectArray5.Select <DynamicMetaObject, Expression>((DynamicMetaObject dmo) => dmo.Expression.Cast(typeof(object))));
                            ConditionalExpression conditionalExpression    = Expression.Condition(methodCallExpression, Expression.Call(parameterExpression1, pSMethodInfoInvoke, expressionArray1), base.GetUpdateExpression(typeof(object)));
                            ParameterExpression[] parameterExpressionArray = new ParameterExpression[1];
                            parameterExpressionArray[0] = parameterExpression;
                            Expression[] expressionArray3 = new Expression[1];
                            expressionArray3[0] = conditionalExpression;
                            return(new DynamicMetaObject(Expression.Block(parameterExpressionArray, expressionArray3), BinderUtils.GetVersionCheck(this._getMemberBinder, this._getMemberBinder._version)).WriteToDebugLog(this));
                        }
                    }
                    else
                    {
                        return(target.ThrowRuntimeError(args, BindingRestrictions.Empty, "InvokeMethodOnNull", ParserStrings.InvokeMethodOnNull, new Expression[0]).WriteToDebugLog(this));
                    }
                }
            }
            return(base.Defer(args.Prepend <DynamicMetaObject>(target).ToArray <DynamicMetaObject>()));
        }
Пример #12
0
        internal static Expression InvokeMethod(MethodInfo mi, DynamicMetaObject target, DynamicMetaObject[] args, bool expandParameters)
        {
            Expression expression;
            List <ParameterExpression> parameterExpressions = new List <ParameterExpression>();
            List <Expression>          expressions          = new List <Expression>();
            var temps     = new List <ParameterExpression>();
            var initTemps = new List <Expression>();
            List <Expression> expressions1 = new List <Expression>();

            ParameterInfo[] parameters      = mi.GetParameters();
            Expression[]    expressionArray = new Expression[(int)parameters.Length];
            for (int i = 0; i < (int)parameters.Length; i++)
            {
                string name      = parameters[i].Name;
                var    paramName = parameters[i].Name;
                if (string.IsNullOrWhiteSpace(parameters[i].Name))
                {
                    paramName = i.ToString(CultureInfo.InvariantCulture);
                }
                if ((int)parameters[i].GetCustomAttributes(typeof(ParamArrayAttribute), false).Length == 0)
                {
                    if (i < (int)args.Length)
                    {
                        if (!parameters[i].ParameterType.IsByRef)
                        {
                            expressionArray[i] = args[i].CastOrConvertMethodArgument(parameters[i].ParameterType, paramName, mi.Name, temps, initTemps);
                        }
                        else
                        {
                            if (args[i].Value as PSReference != null)
                            {
                                ParameterExpression parameterExpression = Expression.Variable(parameters[i].ParameterType.GetElementType());
                                temps.Add(parameterExpression);
                                MemberExpression memberExpression = Expression.Property(args[i].Expression.Cast(typeof(PSReference)), CachedReflectionInfo.PSReference_Value);
                                initTemps.Add(Expression.Assign(parameterExpression, memberExpression.Convert(parameterExpression.Type)));
                                expressions1.Add(Expression.Assign(memberExpression, parameterExpression.Cast(typeof(object))));
                                expressionArray[i] = parameterExpression;
                            }
                            else
                            {
                                Type[] typeArray = new Type[4];
                                typeArray[0] = typeof(string);
                                typeArray[1] = typeof(Exception);
                                typeArray[2] = typeof(string);
                                typeArray[3] = typeof(object[]);
                                object[] nonRefArgumentToRefParameter = new object[4];
                                nonRefArgumentToRefParameter[0] = "NonRefArgumentToRefParameterMsg";
                                nonRefArgumentToRefParameter[2] = ExtendedTypeSystem.NonRefArgumentToRefParameter;
                                object[] fullName = new object[3];
                                fullName[0] = i + 1;
                                fullName[1] = typeof(PSReference).FullName;
                                fullName[2] = "[ref]";
                                nonRefArgumentToRefParameter[3] = fullName;
                                return(Compiler.CreateThrow(typeof(object), typeof(MethodException), typeArray, nonRefArgumentToRefParameter));
                            }
                        }
                    }
                    else
                    {
                        object defaultValue = parameters[i].DefaultValue;
                        if (defaultValue != null)
                        {
                            expressionArray[i] = Expression.Constant(defaultValue);
                        }
                        else
                        {
                            expressionArray[i] = Expression.Constant(null, parameters[i].ParameterType);
                        }
                    }
                }
                else
                {
                    Func <DynamicMetaObject, Expression> func = null;
                    Type elementType = parameters[i].ParameterType.GetElementType();
                    if (!expandParameters)
                    {
                        Expression expression1 = args[i].CastOrConvertMethodArgument(parameters[i].ParameterType, paramName, mi.Name, temps, initTemps);
                        expressionArray[i] = expression1;
                    }
                    else
                    {
                        Expression[] expressionArray1 = expressionArray;
                        int          num  = i;
                        Type         type = elementType;
                        IEnumerable <DynamicMetaObject> dynamicMetaObjects = args.Skip <DynamicMetaObject>(i);
                        if (func == null)
                        {
                            func = (DynamicMetaObject a) => a.CastOrConvertMethodArgument(elementType, name, mi.Name, parameterExpressions, expressions);
                        }
                        expressionArray1[num] = Expression.NewArrayInit(type, dynamicMetaObjects.Select <DynamicMetaObject, Expression>(func));
                    }
                }
            }
            if (mi.IsStatic)
            {
                expression = Expression.Call(mi, expressionArray);
            }
            else
            {
                expression = Expression.Call(PSGetMemberBinder.GetTargetExpr(target).Cast(mi.DeclaringType), mi, expressionArray);
            }
            Expression expression2 = expression;

            if (temps.Any <ParameterExpression>())
            {
                if (!mi.ReturnType.Equals(typeof(void)) && expressions1.Any <Expression>())
                {
                    ParameterExpression parameterExpression1 = Expression.Variable(mi.ReturnType);
                    temps.Add(parameterExpression1);
                    expression2 = Expression.Assign(parameterExpression1, expression2);
                    expressions1.Add(parameterExpression1);
                }
                expression2 = Expression.Block(mi.ReturnType, temps, initTemps.Append <Expression>(expression2).Concat <Expression>(expressions1));
            }
            return(expression2);
        }