コード例 #1
0
        /// <summary>
        /// Returns a CmdletParameterBinderController for the specified command
        /// </summary>
        /// <param name="command">
        /// The cmdlet to bind parameters to.
        /// </param>
        /// <returns>
        /// A new instance of a CmdletParameterBinderController.
        /// </returns>
        /// <exception cref="ArgumentException">
        /// if <paramref name="command"/> is not a Cmdlet.
        /// </exception>
        internal ParameterBinderController NewParameterBinderController(InternalCommand command)
        {
            Cmdlet cmdlet = command as Cmdlet;

            if (cmdlet == null)
            {
                throw PSTraceSource.NewArgumentException("command");
            }

            ParameterBinderBase parameterBinder;
            IScriptCommandInfo  scriptCommandInfo = CommandInfo as IScriptCommandInfo;

            if (scriptCommandInfo != null)
            {
                parameterBinder = new ScriptParameterBinder(scriptCommandInfo.ScriptBlock, cmdlet.MyInvocation, this._context, cmdlet, CommandScope);
            }
            else
            {
                parameterBinder = new ReflectionParameterBinder(cmdlet, cmdlet);
            }

            _cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, CommandInfo.CommandMetadata, parameterBinder);

            return(_cmdletParameterBinderController);
        }
コード例 #2
0
        internal void BindUnboundScriptParameterWithDefaultValue(MergedCompiledCommandParameter parameter)
        {
            ScriptParameterBinder defaultParameterBinder = (ScriptParameterBinder)this.DefaultParameterBinder;
            ScriptBlock           script = defaultParameterBinder.Script;

            if (script.RuntimeDefinedParameters.ContainsKey(parameter.Parameter.Name))
            {
                bool recordBoundParameters = defaultParameterBinder.RecordBoundParameters;
                try
                {
                    defaultParameterBinder.RecordBoundParameters = false;
                    RuntimeDefinedParameter parameter2 = script.RuntimeDefinedParameters[parameter.Parameter.Name];
                    IList implicitUsingParameters      = null;
                    if (this.DefaultParameterBinder.CommandLineParameters != null)
                    {
                        implicitUsingParameters = this.DefaultParameterBinder.CommandLineParameters.GetImplicitUsingParameters();
                    }
                    object defaultScriptParameterValue = defaultParameterBinder.GetDefaultScriptParameterValue(parameter2, implicitUsingParameters);
                    this.SaveDefaultScriptParameterValue(parameter.Parameter.Name, defaultScriptParameterValue);
                    CommandParameterInternal argument       = CommandParameterInternal.CreateParameterWithArgument(PositionUtilities.EmptyExtent, parameter.Parameter.Name, "-" + parameter.Parameter.Name + ":", PositionUtilities.EmptyExtent, defaultScriptParameterValue, false);
                    ParameterBindingFlags    isDefaultValue = ParameterBindingFlags.IsDefaultValue;
                    if (parameter2.IsSet)
                    {
                        isDefaultValue |= ParameterBindingFlags.ShouldCoerceType;
                    }
                    this.BindParameter(int.MaxValue, argument, parameter, isDefaultValue);
                }
                finally
                {
                    defaultParameterBinder.RecordBoundParameters = recordBoundParameters;
                }
            }
        }
コード例 #3
0
        internal ParameterBinderController NewParameterBinderController(InternalCommand command)
        {
            ParameterBinderBase base2;
            Cmdlet cmdlet = command as Cmdlet;

            if (cmdlet == null)
            {
                throw PSTraceSource.NewArgumentException("command");
            }
            IScriptCommandInfo commandInfo = base.CommandInfo as IScriptCommandInfo;

            if (commandInfo != null)
            {
                base2 = new ScriptParameterBinder(commandInfo.ScriptBlock, cmdlet.MyInvocation, base._context, cmdlet, base.CommandScope);
            }
            else
            {
                base2 = new ReflectionParameterBinder(cmdlet, cmdlet);
            }
            this._cmdletParameterBinderController = new System.Management.Automation.CmdletParameterBinderController(cmdlet, base.CommandInfo.CommandMetadata, base2);
            return(this._cmdletParameterBinderController);
        }
コード例 #4
0
ファイル: CommandProcessor.cs プロジェクト: 40a/PowerShell
        // CommandProcessor

#endregion ctor

#region internal members
        /// <summary>
        /// Returns a CmdletParameterBinderController for the specified command
        /// </summary>
        /// 
        /// <param name="command">
        /// The cmdlet to bind parameters to.
        /// </param>
        /// 
        /// <returns>
        /// A new instance of a CmdletParameterBinderController.
        /// </returns>
        /// 
        /// <exception cref="ArgumentException">
        /// if <paramref name="command"/> is not a Cmdlet.
        /// </exception>
        /// 
        internal ParameterBinderController NewParameterBinderController(InternalCommand command)
        {
            Cmdlet cmdlet = command as Cmdlet;
            if (cmdlet == null)
            {
                throw PSTraceSource.NewArgumentException("command");
            }

            ParameterBinderBase parameterBinder;
            IScriptCommandInfo scriptCommandInfo = CommandInfo as IScriptCommandInfo;
            if (scriptCommandInfo != null)
            {
                parameterBinder = new ScriptParameterBinder(scriptCommandInfo.ScriptBlock, cmdlet.MyInvocation, this._context, cmdlet, CommandScope);
            }
            else
            {
                parameterBinder = new ReflectionParameterBinder(cmdlet, cmdlet);
            }

            _cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, CommandInfo.CommandMetadata, parameterBinder);

            return _cmdletParameterBinderController;
        }
コード例 #5
0
        internal virtual bool BindParameter(CommandParameterInternal parameter, CompiledCommandParameter parameterMetadata, ParameterBindingFlags flags)
        {
            bool flag  = false;
            bool flag2 = (flags & ParameterBindingFlags.ShouldCoerceType) != ParameterBindingFlags.None;

            if (parameter == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameter");
            }
            if (parameterMetadata == null)
            {
                throw PSTraceSource.NewArgumentNullException("parameterMetadata");
            }
            using (bindingTracer.TraceScope("BIND arg [{0}] to parameter [{1}]", new object[] { parameter.ArgumentValue, parameterMetadata.Name }))
            {
                parameter.ParameterName = parameterMetadata.Name;
                object argumentValue         = parameter.ArgumentValue;
                ScriptParameterBinder binder = this as ScriptParameterBinder;
                bool bindingScriptCmdlet     = false;
                if (binder != null)
                {
                    bindingScriptCmdlet = binder.Script.UsesCmdletBinding;
                }
                foreach (ArgumentTransformationAttribute attribute in parameterMetadata.ArgumentTransformationAttributes)
                {
                    using (bindingTracer.TraceScope("Executing DATA GENERATION metadata: [{0}]", new object[] { attribute.GetType() }))
                    {
                        try
                        {
                            ArgumentTypeConverterAttribute attribute2 = attribute as ArgumentTypeConverterAttribute;
                            if (attribute2 != null)
                            {
                                if (flag2)
                                {
                                    argumentValue = attribute2.Transform(this.engine, argumentValue, true, bindingScriptCmdlet);
                                }
                            }
                            else
                            {
                                argumentValue = attribute.Transform(this.engine, argumentValue);
                            }
                            bindingTracer.WriteLine("result returned from DATA GENERATION: {0}", new object[] { argumentValue });
                        }
                        catch (Exception exception)
                        {
                            CommandProcessorBase.CheckForSevereException(exception);
                            bindingTracer.WriteLine("ERROR: DATA GENERATION: {0}", new object[] { exception.Message });
                            ParameterBindingException exception2 = new ParameterBindingArgumentTransformationException(exception, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentTransformationError", new object[] { exception.Message });
                            throw exception2;
                        }
                    }
                }
                if (flag2)
                {
                    argumentValue = this.CoerceTypeAsNeeded(parameter, parameterMetadata.Name, parameterMetadata.Type, parameterMetadata.CollectionTypeInformation, argumentValue);
                }
                else if (!this.ShouldContinueUncoercedBind(parameter, parameterMetadata, flags, ref argumentValue))
                {
                    goto Label_040E;
                }
                if ((parameterMetadata.PSTypeName != null) && (argumentValue != null))
                {
                    IEnumerable enumerable = LanguagePrimitives.GetEnumerable(argumentValue);
                    if (enumerable != null)
                    {
                        foreach (object obj3 in enumerable)
                        {
                            this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, obj3);
                        }
                    }
                    else
                    {
                        this.ValidatePSTypeName(parameter, parameterMetadata, !flag2, argumentValue);
                    }
                }
                if ((flags & ParameterBindingFlags.IsDefaultValue) == ParameterBindingFlags.None)
                {
                    foreach (ValidateArgumentsAttribute attribute3 in parameterMetadata.ValidationAttributes)
                    {
                        using (bindingTracer.TraceScope("Executing VALIDATION metadata: [{0}]", new object[] { attribute3.GetType() }))
                        {
                            try
                            {
                                attribute3.InternalValidate(argumentValue, this.engine);
                            }
                            catch (Exception exception3)
                            {
                                CommandProcessorBase.CheckForSevereException(exception3);
                                bindingTracer.WriteLine("ERROR: VALIDATION FAILED: {0}", new object[] { exception3.Message });
                                ParameterBindingValidationException exception4 = new ParameterBindingValidationException(exception3, ErrorCategory.InvalidData, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, (argumentValue == null) ? null : argumentValue.GetType(), "ParameterBinderStrings", "ParameterArgumentValidationError", new object[] { exception3.Message });
                                throw exception4;
                            }
                            tracer.WriteLine("Validation attribute on {0} returned {1}.", new object[] { parameterMetadata.Name, flag });
                        }
                    }
                    if (IsParameterMandatory(parameterMetadata))
                    {
                        this.ValidateNullOrEmptyArgument(parameter, parameterMetadata, parameterMetadata.Type, argumentValue, true);
                    }
                }
                Exception innerException = null;
                try
                {
                    this.BindParameter(parameter.ParameterName, argumentValue);
                    flag = true;
                }
                catch (SetValueException exception6)
                {
                    innerException = exception6;
                }
                if (innerException != null)
                {
                    Type typeSpecified = (argumentValue == null) ? null : argumentValue.GetType();
                    ParameterBindingException exception7 = new ParameterBindingException(innerException, ErrorCategory.WriteError, this.InvocationInfo, this.GetErrorExtent(parameter), parameterMetadata.Name, parameterMetadata.Type, typeSpecified, "ParameterBinderStrings", "ParameterBindingFailed", new object[] { innerException.Message });
                    throw exception7;
                }
                Label_040E :;
                bindingTracer.WriteLine("BIND arg [{0}] to param [{1}] {2}", new object[] { argumentValue, parameter.ParameterName, flag ? "SUCCESSFUL" : "SKIPPED" });
                if (flag)
                {
                    if (this.RecordBoundParameters)
                    {
                        this.CommandLineParameters.Add(parameter.ParameterName, argumentValue);
                    }
                    MshCommandRuntime commandRuntime = this.Command.commandRuntime as MshCommandRuntime;
                    if ((commandRuntime != null) && commandRuntime.LogPipelineExecutionDetail)
                    {
                        IEnumerable source = LanguagePrimitives.GetEnumerable(argumentValue);
                        if (source != null)
                        {
                            string parameterValue = string.Join(", ", source.Cast <object>().ToArray <object>());
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, parameterValue);
                        }
                        else
                        {
                            commandRuntime.PipelineProcessor.LogExecutionParameterBinding(this.InvocationInfo, parameter.ParameterName, (argumentValue == null) ? "" : argumentValue.ToString());
                        }
                    }
                }
                return(flag);
            }
        }
コード例 #6
0
ファイル: CommandProcessor.cs プロジェクト: nickchal/pash
 internal ParameterBinderController NewParameterBinderController(InternalCommand command)
 {
     ParameterBinderBase base2;
     Cmdlet cmdlet = command as Cmdlet;
     if (cmdlet == null)
     {
         throw PSTraceSource.NewArgumentException("command");
     }
     IScriptCommandInfo commandInfo = base.CommandInfo as IScriptCommandInfo;
     if (commandInfo != null)
     {
         base2 = new ScriptParameterBinder(commandInfo.ScriptBlock, cmdlet.MyInvocation, base._context, cmdlet, base.CommandScope);
     }
     else
     {
         base2 = new ReflectionParameterBinder(cmdlet, cmdlet);
     }
     this._cmdletParameterBinderController = new System.Management.Automation.CmdletParameterBinderController(cmdlet, base.CommandInfo.CommandMetadata, base2);
     return this._cmdletParameterBinderController;
 }