예제 #1
0
        } // IsValidValue

        /// <summary>
        /// Runs all ArgumentTransformationAttributes that are specified in the Attributes
        /// collection on the given value in the order that they are in the collection.
        /// </summary>
        /// <param name="attributes">
        /// The attributes to use to transform the value.
        /// </param>
        /// <param name="value">
        /// The value to be transformed.
        /// </param>
        /// <returns>
        /// The transformed value.
        /// </returns>
        /// <exception cref="ArgumentTransformationMetadataException">
        /// If the argument transformation fails.
        /// </exception>
        internal static object TransformValue(IEnumerable <Attribute> attributes, object value)
        {
            Diagnostics.Assert(attributes != null, "caller to verify attributes is not null");

            object result = value;

            // Get an EngineIntrinsics instance using the context of the thread.

            ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS();
            EngineIntrinsics engine  = null;

            if (context != null)
            {
                engine = context.EngineIntrinsics;
            }

            foreach (Attribute attribute in attributes)
            {
                ArgumentTransformationAttribute transformationAttribute =
                    attribute as ArgumentTransformationAttribute;
                if (transformationAttribute != null)
                {
                    result = transformationAttribute.Transform(engine, result);
                }
            }
            return(result);
        }
예제 #2
0
        internal static object TransformValue(IEnumerable <Attribute> attributes, object value)
        {
            object           inputData = value;
            ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
            EngineIntrinsics engineIntrinsics        = null;

            if (executionContextFromTLS != null)
            {
                engineIntrinsics = executionContextFromTLS.EngineIntrinsics;
            }
            foreach (Attribute attribute in attributes)
            {
                ArgumentTransformationAttribute attribute2 = attribute as ArgumentTransformationAttribute;
                if (attribute2 != null)
                {
                    inputData = attribute2.Transform(engineIntrinsics, inputData);
                }
            }
            return(inputData);
        }
예제 #3
0
        private object VerifyNewAttribute(Attribute item)
        {
            object inputData = this.variable.Value;
            ArgumentTransformationAttribute attribute = item as ArgumentTransformationAttribute;

            if (attribute != null)
            {
                ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                EngineIntrinsics engineIntrinsics        = null;
                if (executionContextFromTLS != null)
                {
                    engineIntrinsics = executionContextFromTLS.EngineIntrinsics;
                }
                inputData = attribute.Transform(engineIntrinsics, inputData);
            }
            if (!PSVariable.IsValidValue(inputData, item))
            {
                ValidationMetadataException exception = new ValidationMetadataException("ValidateSetFailure", null, Metadata.InvalidMetadataForCurrentValue, new object[] { this.variable.Name, (this.variable.Value != null) ? this.variable.Value.ToString() : "" });
                throw exception;
            }
            return(inputData);
        }
        /// <summary>
        /// Validates and performs any transformations that the new attribute
        /// implements.
        /// </summary>
        ///
        /// <param name="item">
        /// The new attribute to be added to the collection.
        /// </param>
        ///
        /// <returns>
        /// The new variable value. This may change from the original value if the
        /// new attribute is an ArgumentTransformationAttribute.
        /// </returns>
        ///
        private object VerifyNewAttribute(Attribute item)
        {
            object variableValue = _variable.Value;

            // Perform transformation before validating
            ArgumentTransformationAttribute argumentTransformation = item as ArgumentTransformationAttribute;

            if (argumentTransformation != null)
            {
                // Get an EngineIntrinsics instance using the context of the thread.

                ExecutionContext context = Runspaces.LocalPipeline.GetExecutionContextFromTLS();
                EngineIntrinsics engine  = null;

                if (context != null)
                {
                    engine = context.EngineIntrinsics;
                }

                variableValue = argumentTransformation.Transform(engine, variableValue);
            }

            if (!PSVariable.IsValidValue(variableValue, item))
            {
                ValidationMetadataException e = new ValidationMetadataException(
                    "ValidateSetFailure",
                    null,
                    Metadata.InvalidMetadataForCurrentValue,
                    _variable.Name,
                    ((_variable.Value != null) ? _variable.Value.ToString() : string.Empty));

                throw e;
            }

            return(variableValue);
        }