/// <summary>
 /// Create a scriptmethod from ... a scriptmethod!
 /// </summary>
 /// <param name="Method">The scriptmethod to integrate</param>
 public SelectScriptMethodParameter(PSScriptMethod Method)
 {
     InputObject        = Method;
     Value[Method.Name] = Method.Script;
 }
Exemple #2
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>()));
        }
Exemple #3
0
        internal override IEnumerable <PSTypeName> GetInferredType(CompletionContext context)
        {
            StringConstantExpressionAst member = this.Member as StringConstantExpressionAst;

            if (member != null)
            {
                PSTypeName[] iteratorVariable1;
                if (this.Static)
                {
                    TypeExpressionAst expression = this.Expression as TypeExpressionAst;
                    if (expression == null)
                    {
                        goto Label_064A;
                    }
                    Type type = expression.TypeName.GetReflectionType();
                    if (type == null)
                    {
                        goto Label_064A;
                    }
                    iteratorVariable1 = new PSTypeName[] { new PSTypeName(type) };
                }
                else
                {
                    iteratorVariable1 = this.Expression.GetInferredType(context).ToArray <PSTypeName>();
                    if (iteratorVariable1.Length == 0)
                    {
                        goto Label_064A;
                    }
                }
                List <string> iteratorVariable2 = new List <string> {
                    member.Value
                };
                foreach (PSTypeName iteratorVariable3 in iteratorVariable1)
                {
                    IEnumerable <object> iteratorVariable4 = CompletionCompleters.GetMembersByInferredType(iteratorVariable3, this.Static, context);
                    for (int i = 0; i < iteratorVariable2.Count; i++)
                    {
                        string iteratorVariable6 = iteratorVariable2[i];
                        foreach (object iteratorVariable7 in iteratorVariable4)
                        {
                            PropertyInfo iteratorVariable8 = iteratorVariable7 as PropertyInfo;
                            if (iteratorVariable8 != null)
                            {
                                if (iteratorVariable8.Name.Equals(iteratorVariable6, StringComparison.OrdinalIgnoreCase) && !(this is InvokeMemberExpressionAst))
                                {
                                    yield return(new PSTypeName(iteratorVariable8.PropertyType));

                                    break;
                                }
                            }
                            else
                            {
                                FieldInfo iteratorVariable9 = iteratorVariable7 as FieldInfo;
                                if (iteratorVariable9 != null)
                                {
                                    if (iteratorVariable9.Name.Equals(iteratorVariable6, StringComparison.OrdinalIgnoreCase) && !(this is InvokeMemberExpressionAst))
                                    {
                                        yield return(new PSTypeName(iteratorVariable9.FieldType));

                                        break;
                                    }
                                    continue;
                                }
                                DotNetAdapter.MethodCacheEntry iteratorVariable10 = iteratorVariable7 as DotNetAdapter.MethodCacheEntry;
                                if (iteratorVariable10 != null)
                                {
                                    if (iteratorVariable10[0].method.Name.Equals(iteratorVariable6, StringComparison.OrdinalIgnoreCase))
                                    {
                                        if (!(this is InvokeMemberExpressionAst))
                                        {
                                            yield return(new PSTypeName(typeof(PSMethod)));

                                            break;
                                        }
                                        foreach (MethodInformation iteratorVariable11 in iteratorVariable10.methodInformationStructures)
                                        {
                                            MethodInfo method = iteratorVariable11.method as MethodInfo;
                                            if ((method != null) && !method.ReturnType.ContainsGenericParameters)
                                            {
                                                yield return(new PSTypeName(method.ReturnType));
                                            }
                                        }
                                        break;
                                    }
                                    continue;
                                }
                                PSMemberInfo iteratorVariable13 = iteratorVariable7 as PSMemberInfo;
                                if ((iteratorVariable13 != null) && iteratorVariable13.Name.Equals(iteratorVariable6, StringComparison.OrdinalIgnoreCase))
                                {
                                    PSNoteProperty iteratorVariable14 = iteratorVariable7 as PSNoteProperty;
                                    if (iteratorVariable14 != null)
                                    {
                                        yield return(new PSTypeName(iteratorVariable14.Value.GetType()));

                                        break;
                                    }
                                    PSAliasProperty iteratorVariable15 = iteratorVariable7 as PSAliasProperty;
                                    if (iteratorVariable15 != null)
                                    {
                                        iteratorVariable2.Add(iteratorVariable15.ReferencedMemberName);
                                    }
                                    else
                                    {
                                        PSCodeProperty iteratorVariable16 = iteratorVariable7 as PSCodeProperty;
                                        if (iteratorVariable16 != null)
                                        {
                                            if (iteratorVariable16.GetterCodeReference != null)
                                            {
                                                yield return(new PSTypeName(iteratorVariable16.GetterCodeReference.ReturnType));

                                                break;
                                            }
                                        }
                                        else
                                        {
                                            ScriptBlock      getterScript       = null;
                                            PSScriptProperty iteratorVariable18 = iteratorVariable7 as PSScriptProperty;
                                            if (iteratorVariable18 != null)
                                            {
                                                getterScript = iteratorVariable18.GetterScript;
                                            }
                                            PSScriptMethod iteratorVariable19 = iteratorVariable7 as PSScriptMethod;
                                            if (iteratorVariable19 != null)
                                            {
                                                getterScript = iteratorVariable19.Script;
                                            }
                                            if (getterScript != null)
                                            {
                                                foreach (PSTypeName iteratorVariable20 in getterScript.OutputType)
                                                {
                                                    yield return(iteratorVariable20);
                                                }
                                            }
                                        }
                                    }
                                    break;
                                }
                            }
                        }
                    }
                }
            }
Label_064A:
            yield break;
        }