Esempio n. 1
0
        public void TooManyArgumentsForPSMethodInvoke()
        {
            var          obj      = new Object();
            var          psObject = new PSObject(obj);
            PSMethodInfo method   = psObject.Methods.First(m => m.Name == "ToString");

            MethodException ex = Assert.Throws <MethodException>(() => method.Invoke(1, 2, 3, 4, 5));

            Assert.AreEqual("Cannot find an overload for \"ToString\" and the argument count: \"5\".", ex.Message);
        }
Esempio n. 2
0
        protected override void ProcessRecord()
        {
            string parameterSetName = base.ParameterSetName;

            if (parameterSetName == null)
            {
                return;
            }
            if (!(parameterSetName == "ScriptBlockSet"))
            {
                if (!(parameterSetName == "PropertyAndMethodSet"))
                {
                    return;
                }
            }
            else
            {
                for (int i = this.start; i < this.end; i++)
                {
                    if (this.scripts[i] != null)
                    {
                        this.scripts[i].InvokeUsingCmdlet(this, false, ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, this.InputObject, new object[] { this.InputObject }, AutomationNull.Value, new object[0]);
                    }
                }
                return;
            }
            this.targetString = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectTarget, new object[] { GetStringRepresentation(this.InputObject) });
            if (LanguagePrimitives.IsNull(this.InputObject))
            {
                if ((this._arguments != null) && (this._arguments.Length > 0))
                {
                    base.WriteError(GenerateNameParameterError("InputObject", ParserStrings.InvokeMethodOnNull, "InvokeMethodOnNull", this._inputObject, new object[0]));
                    return;
                }
                string action = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, action))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(GenerateNameParameterError("InputObject", InternalCommandStrings.InputObjectIsNull, "InputObjectIsNull", this._inputObject, new object[0]));
                        return;
                    }
                    base.WriteObject(null);
                }
                return;
            }
            ErrorRecord errorRecord = null;

            if ((this._arguments != null) && (this._arguments.Length > 0))
            {
                this.MethodCallWithArguments();
            }
            else
            {
                if (this.GetValueFromIDictionaryInput())
                {
                    return;
                }
                PSMemberInfo info = null;
                if (WildcardPattern.ContainsWildcardCharacters(this._propertyOrMethodName))
                {
                    ReadOnlyPSMemberInfoCollection <PSMemberInfo> infos = this._inputObject.Members.Match(this._propertyOrMethodName, PSMemberTypes.All);
                    if (infos.Count > 1)
                    {
                        StringBuilder builder = new StringBuilder();
                        foreach (PSMemberInfo info2 in infos)
                        {
                            builder.AppendFormat(CultureInfo.InvariantCulture, " {0}", new object[] { info2.Name });
                        }
                        base.WriteError(GenerateNameParameterError("Name", InternalCommandStrings.AmbiguousPropertyOrMethodName, "AmbiguousPropertyOrMethodName", this._inputObject, new object[] { this._propertyOrMethodName, builder }));
                        return;
                    }
                    if (infos.Count == 1)
                    {
                        info = infos[0];
                    }
                }
                else
                {
                    info = this._inputObject.Members[this._propertyOrMethodName];
                }
                if (info == null)
                {
                    errorRecord = GenerateNameParameterError("Name", InternalCommandStrings.PropertyOrMethodNotFound, "PropertyOrMethodNotFound", this._inputObject, new object[] { this._propertyOrMethodName });
                }
                else
                {
                    if (info is PSMethodInfo)
                    {
                        PSParameterizedProperty property = info as PSParameterizedProperty;
                        if (property != null)
                        {
                            string str2 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { property.Name });
                            if (base.ShouldProcess(this.targetString, str2))
                            {
                                base.WriteObject(info.Value);
                            }
                            return;
                        }
                        PSMethodInfo info3 = info as PSMethodInfo;
                        try
                        {
                            string str3 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectMethodActionWithoutArguments, new object[] { info3.Name });
                            if (base.ShouldProcess(this.targetString, str3) && !this.BlockMethodInLanguageMode(this.InputObject))
                            {
                                object obj2 = info3.Invoke(new object[0]);
                                this.WriteToPipelineWithUnrolling(obj2);
                            }
                            goto Label_0451;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            MethodException exception2 = exception as MethodException;
                            if (((exception2 != null) && (exception2.ErrorRecord != null)) && (exception2.ErrorRecord.FullyQualifiedErrorId == "MethodCountCouldNotFindBest"))
                            {
                                base.WriteObject(info3.Value);
                            }
                            else
                            {
                                base.WriteError(new ErrorRecord(exception, "MethodInvocationError", ErrorCategory.InvalidOperation, this._inputObject));
                            }
                            goto Label_0451;
                        }
                    }
                    string str4 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { info.Name });
                    if (base.ShouldProcess(this.targetString, str4))
                    {
                        try
                        {
                            this.WriteToPipelineWithUnrolling(info.Value);
                        }
                        catch (TerminateException)
                        {
                            throw;
                        }
                        catch (MethodException)
                        {
                            throw;
                        }
                        catch (PipelineStoppedException)
                        {
                            throw;
                        }
                        catch (Exception exception3)
                        {
                            CommandProcessorBase.CheckForSevereException(exception3);
                            base.WriteObject(null);
                        }
                    }
                }
            }
Label_0451:
            if (errorRecord != null)
            {
                string str5 = string.Format(CultureInfo.InvariantCulture, InternalCommandStrings.ForEachObjectPropertyAction, new object[] { this._propertyOrMethodName });
                if (base.ShouldProcess(this.targetString, str5))
                {
                    if (base.Context.IsStrictVersion(2))
                    {
                        base.WriteError(errorRecord);
                        return;
                    }
                    base.WriteObject(null);
                }
            }
        }