internal CmdletParameterBinderController(Cmdlet cmdlet, CommandMetadata commandMetadata, ParameterBinderBase parameterBinder) : base(cmdlet.MyInvocation, cmdlet.Context, parameterBinder) { this._warningSet = new HashSet<string>(); this._useDefaultParameterBinding = true; this._delayBindScriptBlocks = new Dictionary<MergedCompiledCommandParameter, DelayedScriptBlockArgument>(); this._defaultParameterValues = new Dictionary<string, CommandParameterInternal>(StringComparer.OrdinalIgnoreCase); if (cmdlet == null) { throw PSTraceSource.NewArgumentNullException("cmdlet"); } if (commandMetadata == null) { throw PSTraceSource.NewArgumentNullException("commandMetadata"); } this.Command = cmdlet; this._commandRuntime = (MshCommandRuntime)cmdlet.CommandRuntime; this._commandMetadata = commandMetadata; if (commandMetadata.ImplementsDynamicParameters) { base.UnboundParameters = base.BindableParameters.ReplaceMetadata(commandMetadata.StaticCommandParameterMetadata); base.BindableParameters.GenerateParameterSetMappingFromMetadata(commandMetadata.DefaultParameterSetName); } else { base._bindableParameters = commandMetadata.StaticCommandParameterMetadata; base.UnboundParameters = new List<MergedCompiledCommandParameter>(base._bindableParameters.BindableParameters.Values); } }
/// <summary> /// Constructs a parameter binder controller for the specified command /// in the specified engine context. /// </summary> /// /// <param name="invocationInfo"> /// The invocation information about the code being run. /// </param> /// <param name="context"> /// The engine context in which the command is being run. /// </param> /// <param name="parameterBinder"> /// The default parameter binder for the command. /// </param> internal ParameterBinderController(InvocationInfo invocationInfo, ExecutionContext context, ParameterBinderBase parameterBinder) { Diagnostics.Assert(invocationInfo != null, "Caller to verify invocationInfo is not null."); Diagnostics.Assert(parameterBinder != null, "Caller to verify parameterBinder is not null."); Diagnostics.Assert(context != null, "call to verify context is not null."); this.DefaultParameterBinder = parameterBinder; Context = context; InvocationInfo = invocationInfo; }
internal override ParameterBinderController NewParameterBinderController( InternalCommand command) { if (!(command is Cmdlet cmdlet)) { throw CommandProcessor.tracer.NewArgumentException(nameof(command)); } ParameterBinderBase parameterBinder = !(this.CommandInfo is IScriptCommandInfo commandInfo) ? (ParameterBinderBase) new ReflectionParameterBinder((object)cmdlet, cmdlet) : (ParameterBinderBase) new ScriptParameterBinder(commandInfo.ScriptBlock, cmdlet.MyInvocation, this.context, (InternalCommand)cmdlet); this.cmdletParameterBinderController = new CmdletParameterBinderController(cmdlet, this.CommandInfo.CommandMetadata, parameterBinder); return((ParameterBinderController)this.cmdletParameterBinderController); }
/// <summary> /// Initializes the cmdlet parameter binder controller for /// the specified cmdlet and engine context /// </summary> /// /// <param name="cmdlet"> /// The cmdlet that the parameters will be bound to. /// </param> /// /// <param name="commandMetadata"> /// The metadata about the cmdlet. /// </param> /// /// <param name="parameterBinder"> /// The default parameter binder to use. /// </param> /// internal CmdletParameterBinderController( Cmdlet cmdlet, CommandMetadata commandMetadata, ParameterBinderBase parameterBinder) : base( cmdlet.MyInvocation, cmdlet.Context, parameterBinder) { if (cmdlet == null) { throw PSTraceSource.NewArgumentNullException("cmdlet"); } if (commandMetadata == null) { throw PSTraceSource.NewArgumentNullException("commandMetadata"); } this.Command = cmdlet; _commandRuntime = (MshCommandRuntime)cmdlet.CommandRuntime; _commandMetadata = commandMetadata; // Add the static parameter metadata to the bindable parameters // And add them to the unbound parameters list if (commandMetadata.ImplementsDynamicParameters) { // ReplaceMetadata makes a copy for us, so we can use that collection as is. this.UnboundParameters = this.BindableParameters.ReplaceMetadata(commandMetadata.StaticCommandParameterMetadata); } else { _bindableParameters = commandMetadata.StaticCommandParameterMetadata; // Must make a copy of the list because we'll modify it. this.UnboundParameters = new List<MergedCompiledCommandParameter>(_bindableParameters.BindableParameters.Values); } }
internal ParameterBinderController( InvocationInfo invocationInfo, ExecutionContext context, ParameterBinderBase parameterBinder, InternalCommand command) { if (invocationInfo == null) { throw ParameterBinderController.tracer.NewArgumentNullException(nameof(invocationInfo)); } if (parameterBinder == null) { throw ParameterBinderController.tracer.NewArgumentNullException(nameof(parameterBinder)); } if (context == null) { throw ParameterBinderController.tracer.NewArgumentNullException(nameof(context)); } this.command = command; this.parameterBinder = parameterBinder; this.context = context; this.invocationInfo = invocationInfo; }
internal object Transform(EngineIntrinsics engineIntrinsics, object inputData, bool bindingParameters, bool bindingScriptCmdlet) { if (_convertTypes == null) { return(inputData); } object result = inputData; try { for (int i = 0; i < _convertTypes.Length; i++) { if (bindingParameters) { // We should not be doing a conversion here if [ref] is the last type. // When [ref] appears in an argument list, it is used for checking only. // No Conversion should be done. if (_convertTypes[i].Equals(typeof(System.Management.Automation.PSReference))) { object temp; PSObject mshObject = result as PSObject; if (mshObject != null) { temp = mshObject.BaseObject; } else { temp = result; } PSReference reference = temp as PSReference; if (reference == null) { throw new PSInvalidCastException("InvalidCastExceptionReferenceTypeExpected", null, ExtendedTypeSystem.ReferenceTypeExpected); } } else { object temp; PSObject mshObject = result as PSObject; if (mshObject != null) { temp = mshObject.BaseObject; } else { temp = result; } // If a non-ref type is expected but currently passed in is a ref, do an implicit dereference. PSReference reference = temp as PSReference; if (reference != null) { result = reference.Value; } if (bindingScriptCmdlet && _convertTypes[i] == typeof(string)) { // Don't allow conversion from array to string in script w/ cmdlet binding. Allow // the conversion for ordinary script parameter binding for V1 compatibility. temp = PSObject.Base(result); if (temp != null && temp.GetType().IsArray) { throw new PSInvalidCastException("InvalidCastFromAnyTypeToString", null, ExtendedTypeSystem.InvalidCastCannotRetrieveString); } } } } //BUGBUG //NTRAID#Windows Out of Band Releases - 930116 - 03/14/06 //handling special case for boolean, switchparameter and Nullable<bool> //These parameter types will not be converted if the incoming value types are not //one of the accepted categories - $true/$false or numbers (0 or otherwise) if (LanguagePrimitives.IsBoolOrSwitchParameterType(_convertTypes[i])) { CheckBoolValue(result, _convertTypes[i]); } if (bindingScriptCmdlet) { // Check for conversion to something like bool[] or ICollection<bool>, but only for cmdlet binding // to stay compatible with V1. ParameterCollectionTypeInformation collectionTypeInfo = new ParameterCollectionTypeInformation(_convertTypes[i]); if (collectionTypeInfo.ParameterCollectionType != ParameterCollectionType.NotCollection && LanguagePrimitives.IsBoolOrSwitchParameterType(collectionTypeInfo.ElementType)) { IList currentValueAsIList = ParameterBinderBase.GetIList(result); if (currentValueAsIList != null) { foreach (object val in currentValueAsIList) { CheckBoolValue(val, collectionTypeInfo.ElementType); } } else { CheckBoolValue(result, collectionTypeInfo.ElementType); } } } result = LanguagePrimitives.ConvertTo(result, _convertTypes[i], CultureInfo.InvariantCulture); // Do validation of invalid direct variable assignments which are allowed to // be used for parameters. // // Note - this is duplicated in ExecutionContext.cs as parameter binding for script cmdlets can avoid this code path. if ((!bindingScriptCmdlet) && (!bindingParameters)) { // ActionPreference of Suspend is not supported as a preference variable. We can only block "Suspend" // during variable assignment (here) - "Ignore" is blocked during variable retrieval. if (_convertTypes[i] == typeof(ActionPreference)) { ActionPreference resultPreference = (ActionPreference)result; if (resultPreference == ActionPreference.Suspend) { throw new PSInvalidCastException("InvalidActionPreference", null, ErrorPackage.UnsupportedPreferenceVariable, resultPreference); } } } } } catch (PSInvalidCastException e) { throw new ArgumentTransformationMetadataException(e.Message, e); } return(result); }
private void HandleCommandLineDynamicParameters(out ParameterBindingException outgoingBindingException) { outgoingBindingException = null; if (this._commandMetadata.ImplementsDynamicParameters) { using (ParameterBinderBase.bindingTracer.TraceScope("BIND cmd line args to DYNAMIC parameters.", new object[0])) { _tracer.WriteLine("The Cmdlet supports the dynamic parameter interface", new object[0]); IDynamicParameters command = this.Command as IDynamicParameters; if (command != null) { if (this._dynamicParameterBinder == null) { object dynamicParameters; _tracer.WriteLine("Getting the bindable object from the Cmdlet", new object[0]); PSScriptCmdlet cmdlet = this.Command as PSScriptCmdlet; if (cmdlet != null) { cmdlet.PrepareForBinding(((ScriptParameterBinder)base.DefaultParameterBinder).LocalScope, base.CommandLineParameters); } try { dynamicParameters = command.GetDynamicParameters(); } catch (Exception exception) { CommandProcessorBase.CheckForSevereException(exception); if (exception is ProviderInvocationException) { throw; } ParameterBindingException exception2 = new ParameterBindingException(exception, ErrorCategory.InvalidArgument, this.Command.MyInvocation, null, null, null, null, "ParameterBinderStrings", "GetDynamicParametersException", new object[] { exception.Message }); throw exception2; } if (dynamicParameters != null) { InternalParameterMetadata metadata; ParameterBinderBase.bindingTracer.WriteLine("DYNAMIC parameter object: [{0}]", new object[] { dynamicParameters.GetType() }); _tracer.WriteLine("Creating a new parameter binder for the dynamic parameter object", new object[0]); RuntimeDefinedParameterDictionary runtimeDefinedParameters = dynamicParameters as RuntimeDefinedParameterDictionary; if (runtimeDefinedParameters != null) { metadata = InternalParameterMetadata.Get(runtimeDefinedParameters, true, true); this._dynamicParameterBinder = new RuntimeDefinedParameterBinder(runtimeDefinedParameters, this.Command, base.CommandLineParameters); } else { metadata = InternalParameterMetadata.Get(dynamicParameters.GetType(), base.Context, true); this._dynamicParameterBinder = new ReflectionParameterBinder(dynamicParameters, this.Command, base.CommandLineParameters); } foreach (MergedCompiledCommandParameter parameter in base.BindableParameters.AddMetadataForBinder(metadata, ParameterBinderAssociation.DynamicParameters)) { base.UnboundParameters.Add(parameter); } this._commandMetadata.DefaultParameterSetFlag = base.BindableParameters.GenerateParameterSetMappingFromMetadata(this._commandMetadata.DefaultParameterSetName); } } if (this._dynamicParameterBinder == null) { _tracer.WriteLine("No dynamic parameter object was returned from the Cmdlet", new object[0]); } else if (base.UnboundArguments.Count > 0) { using (ParameterBinderBase.bindingTracer.TraceScope("BIND NAMED args to DYNAMIC parameters", new object[0])) { base.ReparseUnboundArguments(); base.UnboundArguments = this.BindParameters(base._currentParameterSetFlag, base.UnboundArguments); } using (ParameterBinderBase.bindingTracer.TraceScope("BIND POSITIONAL args to DYNAMIC parameters", new object[0])) { base.UnboundArguments = base.BindPositionalParameters(base.UnboundArguments, base._currentParameterSetFlag, this._commandMetadata.DefaultParameterSetFlag, out outgoingBindingException); } } } } } }
internal ParameterBinderController(System.Management.Automation.InvocationInfo invocationInfo, ExecutionContext context, ParameterBinderBase parameterBinder) { this.DefaultParameterBinder = parameterBinder; this._context = context; this._invocationInfo = invocationInfo; }
internal object Transform(EngineIntrinsics engineIntrinsics, object inputData, bool bindingParameters, bool bindingScriptCmdlet) { if (this._convertTypes == null) { return(inputData); } object obj2 = inputData; try { for (int i = 0; i < this._convertTypes.Length; i++) { if (bindingParameters) { if (this._convertTypes[i].Equals(typeof(PSReference))) { object baseObject; PSObject obj4 = obj2 as PSObject; if (obj4 != null) { baseObject = obj4.BaseObject; } else { baseObject = obj2; } if (!(baseObject is PSReference)) { throw new PSInvalidCastException("InvalidCastExceptionReferenceTypeExpected", null, ExtendedTypeSystem.ReferenceTypeExpected, new object[0]); } } else { object obj5; PSObject obj6 = obj2 as PSObject; if (obj6 != null) { obj5 = obj6.BaseObject; } else { obj5 = obj2; } PSReference reference2 = obj5 as PSReference; if (reference2 != null) { obj2 = reference2.Value; } if (bindingScriptCmdlet && (this._convertTypes[i] == typeof(string))) { obj5 = PSObject.Base(obj2); if ((obj5 != null) && obj5.GetType().IsArray) { throw new PSInvalidCastException("InvalidCastFromAnyTypeToString", null, ExtendedTypeSystem.InvalidCastCannotRetrieveString, new object[0]); } } } } if (LanguagePrimitives.IsBoolOrSwitchParameterType(this._convertTypes[i])) { CheckBoolValue(obj2, this._convertTypes[i]); } if (bindingScriptCmdlet) { ParameterCollectionTypeInformation information = new ParameterCollectionTypeInformation(this._convertTypes[i]); if ((information.ParameterCollectionType != ParameterCollectionType.NotCollection) && LanguagePrimitives.IsBoolOrSwitchParameterType(information.ElementType)) { IList iList = ParameterBinderBase.GetIList(obj2); if (iList != null) { foreach (object obj7 in iList) { CheckBoolValue(obj7, information.ElementType); } } else { CheckBoolValue(obj2, information.ElementType); } } } obj2 = LanguagePrimitives.ConvertTo(obj2, this._convertTypes[i], CultureInfo.InvariantCulture); } } catch (PSInvalidCastException exception) { throw new ArgumentTransformationMetadataException(exception.Message, exception); } return(obj2); }