internal static PSReference GetVariableAsRef(VariablePath variablePath, ExecutionContext executionContext, Type staticType) { Diagnostics.Assert(variablePath.IsVariable, "calller to verify varpath is a variable."); SessionStateInternal sessionState = executionContext.EngineSessionState; CommandOrigin origin = sessionState.CurrentScope.ScopeOrigin; SessionStateScope scope; PSVariable var = sessionState.GetVariableItem(variablePath, out scope, origin); if (var == null) { throw InterpreterError.NewInterpreterException(variablePath, typeof(RuntimeException), null, "NonExistingVariableReference", ParserStrings.NonExistingVariableReference); } object value = var.Value; if (staticType == null && value != null) { value = PSObject.Base(value); if (value != null) { staticType = value.GetType(); } } if (staticType == null) { var declaredType = var.Attributes.OfType <ArgumentTypeConverterAttribute>().FirstOrDefault(); staticType = declaredType != null ? declaredType.TargetType : typeof(LanguagePrimitives.Null); } return(PSReference.CreateInstance(var, staticType)); }
private static PSReference GetVariableAsRef(VariablePath variablePath, ExecutionContext executionContext, Type staticType) { SessionStateScope scope; SessionStateInternal engineSessionState = executionContext.EngineSessionState; CommandOrigin scopeOrigin = engineSessionState.CurrentScope.ScopeOrigin; PSVariable variable = engineSessionState.GetVariableItem(variablePath, out scope, scopeOrigin); if (variable == null) { throw InterpreterError.NewInterpreterException(variablePath, typeof(RuntimeException), null, "NonExistingVariableReference", ParserStrings.NonExistingVariableReference, new object[0]); } object obj2 = variable.Value; if ((staticType == null) && (obj2 != null)) { obj2 = PSObject.Base(obj2); if (obj2 != null) { staticType = obj2.GetType(); } } if (staticType == null) { ArgumentTypeConverterAttribute attribute = variable.Attributes.OfType <ArgumentTypeConverterAttribute>().FirstOrDefault <ArgumentTypeConverterAttribute>(); staticType = (attribute != null) ? attribute.TargetType : typeof(LanguagePrimitives.Null); } return(PSReference.CreateInstance(variable, staticType)); }
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); }
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); }