public override object Transform(EngineIntrinsics engineIntrinsics, object inputData) { object argument = PSObject.Base(inputData); // deal with scalar string argument var strArg = argument as string; if (strArg != null) { return(new PSModuleInfo(name: strArg, path: null, context: null, sessionState: null)); } // deal with IList argument IList iListArg = ParameterBinderBase.GetIList(argument); if (iListArg != null && iListArg.Count > 0) { int elemtCount = iListArg.Count; int targetIndex = 0; var target = Array.CreateInstance(typeof(object), elemtCount); foreach (object elemt in iListArg) { var elemtValue = PSObject.Base(elemt); if (elemtValue is PSModuleInfo) { target.SetValue(elemtValue, targetIndex++); } else if (elemtValue is string) { var elemtAsModuleObj = new PSModuleInfo(name: (string)elemtValue, path: null, context: null, sessionState: null); target.SetValue(elemtAsModuleObj, targetIndex++); } else { PSModuleInfo elementValueModuleInfo = null; if (TryConvertFromDeserializedModuleInfo(elemtValue, out elementValueModuleInfo)) { target.SetValue(elementValueModuleInfo, targetIndex++); } else { target.SetValue(elemt, targetIndex++); } } } return(target); } PSModuleInfo moduleInfo = null; if (TryConvertFromDeserializedModuleInfo(inputData, out moduleInfo)) { return(moduleInfo); } return(inputData); }
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; } if (!(temp is PSReference reference)) { 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.Suspend is reserved for future use and is not supported as a preference variable. if (_convertTypes[i] == typeof(ActionPreference)) { ActionPreference resultPreference = (ActionPreference)result; if (resultPreference == ActionPreference.Suspend) { throw new PSInvalidCastException("InvalidActionPreference", null, ErrorPackage.ActionPreferenceReservedForFutureUseError, resultPreference); } } } } } catch (PSInvalidCastException e) { throw new ArgumentTransformationMetadataException(e.Message, e); } // Track the flow of untrusted object during the conversion when it's called directly from ParameterBinderBase. // When it's called from the override Transform method, the tracking is taken care of in the base type. if (bindingParameters || bindingScriptCmdlet) { ExecutionContext.PropagateInputSource(inputData, result, engineIntrinsics.SessionState.Internal.LanguageMode); } return(result); }