/// <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);
        }
Example #2
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);
        }
        } // HandleRemainingArguments


        /// <summary>
        /// Determines if the cmdlet supports dynamic parameters. If it does,
        /// the dynamic parameter bindable object is retrieved and the unbound
        /// arguments are bound to it.
        /// </summary>
        /// 
        /// <param name="outgoingBindingException">
        /// Returns the underlying parameter binding exception if any was generated.
        /// </param>
        /// 
        /// <exception cref="MetadataException">
        /// If there was an error compiling the parameter metadata.
        /// </exception>
        /// 
        /// <exception cref="ParameterBindingException">
        /// If there was an error binding the arguments to the parameters.
        /// </exception>
        /// 
        private void HandleCommandLineDynamicParameters(out ParameterBindingException outgoingBindingException)
        {
            outgoingBindingException = null;

            if (_commandMetadata.ImplementsDynamicParameters)
            {
                using (ParameterBinderBase.bindingTracer.TraceScope(
                    "BIND cmd line args to DYNAMIC parameters."))
                {
                    s_tracer.WriteLine("The Cmdlet supports the dynamic parameter interface");

                    IDynamicParameters dynamicParameterCmdlet = this.Command as IDynamicParameters;

                    if (dynamicParameterCmdlet != null)
                    {
                        if (_dynamicParameterBinder == null)
                        {
                            s_tracer.WriteLine("Getting the bindable object from the Cmdlet");

                            var psCompiledScriptCmdlet = this.Command as PSScriptCmdlet;
                            if (psCompiledScriptCmdlet != null)
                            {
                                psCompiledScriptCmdlet.PrepareForBinding(
                                    ((ScriptParameterBinder)this.DefaultParameterBinder).LocalScope,
                                    this.CommandLineParameters);
                            }

                            // Now get the dynamic parameter bindable object.
                            object dynamicParamBindableObject;

                            try
                            {
                                dynamicParamBindableObject = dynamicParameterCmdlet.GetDynamicParameters();
                            }
                            catch (Exception e) // Catch-all OK, this is a third-party callout
                            {
                                CommandProcessorBase.CheckForSevereException(e);

                                if (e is ProviderInvocationException) { throw; }

                                ParameterBindingException bindingException =
                                    new ParameterBindingException(
                                        e,
                                        ErrorCategory.InvalidArgument,
                                        this.Command.MyInvocation,
                                        null,
                                        null,
                                        null,
                                        null,
                                        ParameterBinderStrings.GetDynamicParametersException,
                                        "GetDynamicParametersException",
                                        e.Message);

                                // This exception is caused because failure happens when retrieving the dynamic parameters,
                                // this is not caused by introducing the default parameter binding.
                                throw bindingException;
                            }

                            if (dynamicParamBindableObject != null)
                            {
                                ParameterBinderBase.bindingTracer.WriteLine(
                                    "DYNAMIC parameter object: [{0}]",
                                    dynamicParamBindableObject.GetType());

                                s_tracer.WriteLine("Creating a new parameter binder for the dynamic parameter object");

                                InternalParameterMetadata dynamicParameterMetadata;

                                RuntimeDefinedParameterDictionary runtimeParamDictionary = dynamicParamBindableObject as RuntimeDefinedParameterDictionary;
                                if (runtimeParamDictionary != null)
                                {
                                    // Generate the type metadata for the runtime-defined parameters
                                    dynamicParameterMetadata =
                                        InternalParameterMetadata.Get(runtimeParamDictionary, true, true);

                                    _dynamicParameterBinder =
                                        new RuntimeDefinedParameterBinder(
                                            runtimeParamDictionary,
                                            this.Command,
                                            this.CommandLineParameters);
                                }
                                else
                                {
                                    // Generate the type metadata or retrieve it from the cache
                                    dynamicParameterMetadata =
                                        InternalParameterMetadata.Get(dynamicParamBindableObject.GetType(), Context, true);

                                    // Create the parameter binder for the dynamic parameter object

                                    _dynamicParameterBinder =
                                        new ReflectionParameterBinder(
                                            dynamicParamBindableObject,
                                            this.Command,
                                            this.CommandLineParameters);
                                }

                                // Now merge the metadata with other metadata for the command

                                var dynamicParams =
                                    BindableParameters.AddMetadataForBinder(
                                        dynamicParameterMetadata,
                                        ParameterBinderAssociation.DynamicParameters);
                                foreach (var param in dynamicParams)
                                {
                                    UnboundParameters.Add(param);
                                }

                                // Now set the parameter set flags for the new type metadata.
                                _commandMetadata.DefaultParameterSetFlag =
                                    this.BindableParameters.GenerateParameterSetMappingFromMetadata(_commandMetadata.DefaultParameterSetName);
                            }
                        }

                        if (_dynamicParameterBinder == null)
                        {
                            s_tracer.WriteLine("No dynamic parameter object was returned from the Cmdlet");
                            return;
                        }

                        if (UnboundArguments.Count > 0)
                        {
                            using (ParameterBinderBase.bindingTracer.TraceScope(
                                    "BIND NAMED args to DYNAMIC parameters"))
                            {
                                // Try to bind the unbound arguments as static parameters to the 
                                // dynamic parameter object.

                                ReparseUnboundArguments();

                                UnboundArguments = BindParameters(_currentParameterSetFlag, UnboundArguments);
                            }

                            using (ParameterBinderBase.bindingTracer.TraceScope(
                                    "BIND POSITIONAL args to DYNAMIC parameters"))
                            {
                                UnboundArguments =
                                    BindPositionalParameters(
                                    UnboundArguments,
                                    _currentParameterSetFlag,
                                    _commandMetadata.DefaultParameterSetFlag,
                                    out outgoingBindingException);
                            }
                        }
                    } // dynamicParameterCmdlet != null
                }
            }
        } // HandleCommandLineDynamicParameters
Example #4
0
        // 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;
        }
Example #5
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;
 }