/// <summary> /// Entry point used by the engine to reads the input pipeline object /// and binds the parameters. /// /// This default implementation reads the next pipeline object and sets /// it as the CurrentPipelineObject in the InternalCommand. /// Does not throw. /// </summary> /// <returns> /// True if read succeeds. /// </returns> internal virtual bool Read() { // Prepare the default value parameter list if this is the first call to Read if (_firstCallToRead) { _firstCallToRead = false; } // Retrieve the object from the input pipeline object inputObject = this.commandRuntime.InputPipe.Retrieve(); if (inputObject == AutomationNull.Value) { return(false); } // If we are reading input for the first command in the pipeline increment PipelineIterationInfo[0], which is the number of items read from the input if (this.Command.MyInvocation.PipelinePosition == 1) { this.Command.MyInvocation.PipelineIterationInfo[0]++; } Command.CurrentPipelineObject = LanguagePrimitives.AsPSObjectOrNull(inputObject); return(true); }
private Attribute GetCustomAttribute() { Attribute customAttributeObject = this.GetCustomAttributeObject(); if (customAttributeObject != null && this._namedArguments != null) { Hashtable constValue = (Hashtable)this._namedArguments.GetConstValue(); PSObject psObject = LanguagePrimitives.AsPSObjectOrNull((object)customAttributeObject); foreach (object key in (IEnumerable)constValue.Keys) { object obj = constValue[key]; try { PSMemberInfo member = psObject.Members[key.ToString()]; if (member != null) { member.Value = obj; } else { throw InterpreterError.NewInterpreterException((object)this.NodeToken, typeof(RuntimeException), this.NodeToken, "PropertyNotFoundForType", (object)key.ToString(), (object)customAttributeObject.GetType().ToString()); } } catch (Exception ex) { CommandProcessorBase.CheckForSevereException(ex); throw InterpreterError.NewInterpreterExceptionByMessage(typeof(RuntimeException), this.NodeToken, ex.Message, "PropertyAssignmentException", ex); } } } return(customAttributeObject); }
private PSObject GetTarget(Array input, ExecutionContext context) { if (this._target is VariableDereferenceNode target) { target.GetVariable(context)?.WrapValue(); } return(LanguagePrimitives.AsPSObjectOrNull(this._target.Execute(input, (Pipe)null, context))); }
private Collection <PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args) { object obj2; if (this._cmdlet != null) { this._cmdlet.ThrowIfStopping(); } Cmdlet contextCmdlet = null; ScriptBlock.ErrorHandlingBehavior writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe; if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output) { contextCmdlet = this._cmdlet; writeToPipeline &= ~PipelineResultTypes.Output; } if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error) { writeToExternalErrorPipe = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe; writeToPipeline &= ~PipelineResultTypes.Error; } if (writeToPipeline != PipelineResultTypes.None) { throw PSTraceSource.NewNotImplementedException(); } if (contextCmdlet != null) { sb.InvokeUsingCmdlet(contextCmdlet, useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args); obj2 = AutomationNull.Value; } else { obj2 = sb.DoInvokeReturnAsIs(useNewScope, writeToExternalErrorPipe, AutomationNull.Value, input, AutomationNull.Value, args); } if (obj2 == AutomationNull.Value) { return(new Collection <PSObject>()); } Collection <PSObject> collection = obj2 as Collection <PSObject>; if (collection == null) { collection = new Collection <PSObject>(); IEnumerator enumerator = null; enumerator = LanguagePrimitives.GetEnumerator(obj2); if (enumerator != null) { while (enumerator.MoveNext()) { object current = enumerator.Current; collection.Add(LanguagePrimitives.AsPSObjectOrNull(current)); } return(collection); } collection.Add(LanguagePrimitives.AsPSObjectOrNull(obj2)); } return(collection); }
internal ReflectionParameterBinder( object target, Cmdlet command, CommandLineParameters commandLineParameters) : base((object)LanguagePrimitives.AsPSObjectOrNull(target), command.MyInvocation, command.Context, (InternalCommand)command) { using (ReflectionParameterBinder.tracer.TraceConstructor((object)this)) this.CommandLineParameters = commandLineParameters; }
internal virtual bool Read() { if (this.firstCallToRead) { this.firstCallToRead = false; } object obj2 = this.commandRuntime.InputPipe.Retrieve(); if (obj2 == AutomationNull.Value) { return(false); } if (this.Command.MyInvocation.PipelinePosition == 1) { this.Command.MyInvocation.PipelineIterationInfo[0]++; } this.Command.CurrentPipelineObject = LanguagePrimitives.AsPSObjectOrNull(obj2); return(true); }
private Collection <PSObject> InvokeScript(ScriptBlock sb, bool useNewScope, PipelineResultTypes writeToPipeline, IList input, params object[] args) { if (_cmdlet != null) { _cmdlet.ThrowIfStopping(); } Cmdlet cmdletToUse = null; ScriptBlock.ErrorHandlingBehavior errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe; // Check if they want output if ((writeToPipeline & PipelineResultTypes.Output) == PipelineResultTypes.Output) { cmdletToUse = _cmdlet; writeToPipeline &= (~PipelineResultTypes.Output); } // Check if they want error if ((writeToPipeline & PipelineResultTypes.Error) == PipelineResultTypes.Error) { errorHandlingBehavior = ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe; writeToPipeline &= (~PipelineResultTypes.Error); } if (writeToPipeline != PipelineResultTypes.None) { // The only output types are Output and Error. throw PSTraceSource.NewNotImplementedException(); } // If the cmdletToUse is not null, then the result of the evaluation will be // streamed out the output pipe of the cmdlet. object rawResult; if (cmdletToUse != null) { sb.InvokeUsingCmdlet( contextCmdlet: cmdletToUse, useLocalScope: useNewScope, errorHandlingBehavior: errorHandlingBehavior, dollarUnder: AutomationNull.Value, input: input, scriptThis: AutomationNull.Value, args: args); rawResult = AutomationNull.Value; } else { rawResult = sb.DoInvokeReturnAsIs( useLocalScope: useNewScope, errorHandlingBehavior: errorHandlingBehavior, dollarUnder: AutomationNull.Value, input: input, scriptThis: AutomationNull.Value, args: args); } if (rawResult == AutomationNull.Value) { return(new Collection <PSObject>()); } // If the result is already a collection of PSObjects, just return it... Collection <PSObject> result = rawResult as Collection <PSObject>; if (result != null) { return(result); } result = new Collection <PSObject>(); IEnumerator list = null; list = LanguagePrimitives.GetEnumerator(rawResult); if (list != null) { while (list.MoveNext()) { object val = list.Current; result.Add(LanguagePrimitives.AsPSObjectOrNull(val)); } } else { result.Add(LanguagePrimitives.AsPSObjectOrNull(rawResult)); } return(result); }
protected override void ValidateElement(object element) { if (element == null) { throw new ValidationMetadataException("ArgumentIsEmpty", (Exception)null, "Metadata", "ValidateNotNullFailure", new object[0]); } if (!LanguagePrimitives.IsTrue(this._scriptBlock.DoInvokeReturnAsIs((object)LanguagePrimitives.AsPSObjectOrNull(element), (object)AutomationNull.Value))) { throw new ValidationMetadataException("ValidateScriptFailure", (Exception)null, "Metadata", "ValidateScriptFailure", new object[2] { element, (object)this._scriptBlock }); } }
protected override void ValidateElement(object element) { if (element == null) { throw new ValidationMetadataException("ArgumentIsEmpty", null, Metadata.ValidateNotNullFailure, new object[0]); } if (!LanguagePrimitives.IsTrue(this._scriptBlock.DoInvokeReturnAsIs(true, System.Management.Automation.ScriptBlock.ErrorHandlingBehavior.WriteToExternalErrorPipe, LanguagePrimitives.AsPSObjectOrNull(element), AutomationNull.Value, AutomationNull.Value, new object[0]))) { throw new ValidationMetadataException("ValidateScriptFailure", null, Metadata.ValidateScriptFailure, new object[] { element, this._scriptBlock }); } }
private object CoerceTypeAsNeeded(CommandParameterInternal argument, string parameterName, Type toType, ParameterCollectionTypeInformation collectionTypeInfo, object currentValue) { if (argument == null) { throw PSTraceSource.NewArgumentNullException("argument"); } if (toType == null) { throw PSTraceSource.NewArgumentNullException("toType"); } if (collectionTypeInfo == null) { collectionTypeInfo = new ParameterCollectionTypeInformation(toType); } object result = currentValue; using (bindingTracer.TraceScope("COERCE arg to [{0}]", new object[] { toType })) { Type c = null; try { if (IsNullParameterValue(currentValue)) { return(this.HandleNullParameterForSpecialTypes(argument, parameterName, toType, currentValue)); } c = currentValue.GetType(); if (toType.IsAssignableFrom(c)) { bindingTracer.WriteLine("Parameter and arg types the same, no coercion is needed.", new object[0]); return(currentValue); } bindingTracer.WriteLine("Trying to convert argument value from {0} to {1}", new object[] { c, toType }); if (toType == typeof(PSObject)) { if ((this.command != null) && (currentValue == this.command.CurrentPipelineObject.BaseObject)) { currentValue = this.command.CurrentPipelineObject; } bindingTracer.WriteLine("The parameter is of type [{0}] and the argument is an PSObject, so the parameter value is the argument value wrapped into an PSObject.", new object[] { toType }); return(LanguagePrimitives.AsPSObjectOrNull(currentValue)); } if ((toType == typeof(string)) && (c == typeof(PSObject))) { PSObject obj3 = (PSObject)currentValue; if (obj3 == AutomationNull.Value) { bindingTracer.WriteLine("CONVERT a null PSObject to a null string.", new object[0]); return(null); } } if (((toType == typeof(bool)) || (toType == typeof(SwitchParameter))) || (toType == typeof(bool?))) { Type type = null; if (c == typeof(PSObject)) { PSObject obj4 = (PSObject)currentValue; currentValue = obj4.BaseObject; if (currentValue is SwitchParameter) { SwitchParameter parameter = (SwitchParameter)currentValue; currentValue = parameter.IsPresent; } type = currentValue.GetType(); } else { type = c; } if (type == typeof(bool)) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject((bool)currentValue)); } return(new SwitchParameter((bool)currentValue)); } if (type == typeof(int)) { if (((int)LanguagePrimitives.ConvertTo(currentValue, typeof(int), CultureInfo.InvariantCulture)) != 0) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(true)); } return(new SwitchParameter(true)); } if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(false)); } return(new SwitchParameter(false)); } if (LanguagePrimitives.IsNumeric(Type.GetTypeCode(type))) { double num = (double)LanguagePrimitives.ConvertTo(currentValue, typeof(double), CultureInfo.InvariantCulture); if (num == 0.0) { if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(false)); } return(new SwitchParameter(false)); } if (LanguagePrimitives.IsBooleanType(toType)) { return(ParserOps.BoolToObject(true)); } return(new SwitchParameter(true)); } ParameterBindingException exception = new ParameterBindingException(ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgument", new object[] { type, "" }); throw exception; } if ((collectionTypeInfo.ParameterCollectionType == ParameterCollectionType.ICollectionGeneric) || (collectionTypeInfo.ParameterCollectionType == ParameterCollectionType.IList)) { object obj5 = PSObject.Base(currentValue); if (obj5 != null) { ConversionRank conversionRank = LanguagePrimitives.GetConversionRank(obj5.GetType(), toType); if ((((conversionRank == ConversionRank.Constructor) || (conversionRank == ConversionRank.ImplicitCast)) || (conversionRank == ConversionRank.ExplicitCast)) && LanguagePrimitives.TryConvertTo(currentValue, toType, Thread.CurrentThread.CurrentCulture, out result)) { return(result); } } } if (collectionTypeInfo.ParameterCollectionType != ParameterCollectionType.NotCollection) { bindingTracer.WriteLine("ENCODING arg into collection", new object[0]); bool coercionRequired = false; return(this.EncodeCollection(argument, parameterName, collectionTypeInfo, toType, currentValue, collectionTypeInfo.ElementType != null, out coercionRequired)); } if (((((GetIList(currentValue) != null) && (toType != typeof(object))) && ((toType != typeof(PSObject)) && (toType != typeof(PSListModifier)))) && ((!toType.IsGenericType || (toType.GetGenericTypeDefinition() != typeof(PSListModifier <>))) && (!toType.IsGenericType || (toType.GetGenericTypeDefinition() != typeof(FlagsExpression <>))))) && !toType.IsEnum) { throw new NotSupportedException(); } bindingTracer.WriteLine("CONVERT arg type to param type using LanguagePrimitives.ConvertTo", new object[0]); bool flag2 = false; if (this.context.LanguageMode == PSLanguageMode.ConstrainedLanguage) { object obj6 = PSObject.Base(currentValue); bool flag3 = obj6 is PSObject; bool flag4 = (obj6 != null) && typeof(IDictionary).IsAssignableFrom(obj6.GetType()); flag2 = ((((PSLanguageMode)this.Command.CommandInfo.DefiningLanguageMode) == PSLanguageMode.FullLanguage) && !flag3) && !flag4; } try { if (flag2) { this.context.LanguageMode = PSLanguageMode.FullLanguage; } result = LanguagePrimitives.ConvertTo(currentValue, toType, Thread.CurrentThread.CurrentCulture); } finally { if (flag2) { this.context.LanguageMode = PSLanguageMode.ConstrainedLanguage; } } bindingTracer.WriteLine("CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [{0}]", new object[] { (result == null) ? "null" : result.ToString() }); return(result); } catch (NotSupportedException exception2) { bindingTracer.TraceError("ERROR: COERCE FAILED: arg [{0}] could not be converted to the parameter type [{1}]", new object[] { (result == null) ? "null" : result, toType }); ParameterBindingException exception3 = new ParameterBindingException(exception2, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgument", new object[] { (result == null) ? "null" : result, exception2.Message }); throw exception3; } catch (PSInvalidCastException exception4) { object[] args = new object[] { result ?? "null", toType }; bindingTracer.TraceError("ERROR: COERCE FAILED: arg [{0}] could not be converted to the parameter type [{1}]", args); ParameterBindingException exception5 = new ParameterBindingException(exception4, ErrorCategory.InvalidArgument, this.InvocationInfo, this.GetErrorExtent(argument), parameterName, toType, c, "ParameterBinderStrings", "CannotConvertArgumentNoMessage", new object[] { exception4.Message }); throw exception5; } } return(result); }