} // 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
 private void ProcessAttribute(string memberName, Attribute attribute)
 {
     if (attribute != null)
     {
         this.attributes.Add(attribute);
         ParameterAttribute parameter = attribute as ParameterAttribute;
         if (parameter != null)
         {
             this.ProcessParameterAttribute(memberName, parameter);
         }
         else
         {
             AliasAttribute attribute3 = attribute as AliasAttribute;
             if (attribute3 != null)
             {
                 this.ProcessAliasAttribute(attribute3);
             }
             else
             {
                 ArgumentTransformationAttribute item = attribute as ArgumentTransformationAttribute;
                 if (item != null)
                 {
                     this.argumentTransformationAttributes.Add(item);
                 }
                 else
                 {
                     ValidateArgumentsAttribute attribute5 = attribute as ValidateArgumentsAttribute;
                     if (attribute5 != null)
                     {
                         this.validationAttributes.Add(attribute5);
                     }
                     else if (attribute is AllowNullAttribute)
                     {
                         this.allowsNullArgument = true;
                     }
                     else if (attribute is AllowEmptyStringAttribute)
                     {
                         this.allowsEmptyStringArgument = true;
                     }
                     else if (attribute is AllowEmptyCollectionAttribute)
                     {
                         this.allowsEmptyCollectionArgument = true;
                     }
                     else
                     {
                         PSTypeNameAttribute attribute9 = attribute as PSTypeNameAttribute;
                         if (attribute9 != null)
                         {
                             this.PSTypeName = attribute9.PSTypeName;
                         }
                     }
                 }
             }
         }
     }
 }
示例#3
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);
        }
示例#4
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);
        }
示例#5
0
        /// <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.TransformInternal(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);
        }
示例#6
0
        /// <summary>
        /// Processes the Attribute metadata to generate a CompiledCommandAttribute.
        /// </summary>
        /// <exception cref="MetadataException">
        /// If the attribute is a parameter attribute and another parameter attribute
        /// has been processed with the same parameter-set name.
        /// </exception>
        private void ProcessAttribute(
            string memberName,
            Attribute attribute,
            ref Collection <ValidateArgumentsAttribute> validationAttributes,
            ref Collection <ArgumentTransformationAttribute> argTransformationAttributes,
            ref string[] aliases)
        {
            if (attribute == null)
            {
                return;
            }

            CompiledAttributes.Add(attribute);

            // Now process the attribute based on it's type
            if (attribute is ParameterAttribute paramAttr)
            {
                ProcessParameterAttribute(memberName, paramAttr);
                return;
            }

            ValidateArgumentsAttribute validateAttr = attribute as ValidateArgumentsAttribute;

            if (validateAttr != null)
            {
                if (validationAttributes == null)
                {
                    validationAttributes = new Collection <ValidateArgumentsAttribute>();
                }
                validationAttributes.Add(validateAttr);
                if ((attribute is ValidateNotNullAttribute) || (attribute is ValidateNotNullOrEmptyAttribute))
                {
                    this.CannotBeNull = true;
                }

                return;
            }

            AliasAttribute aliasAttr = attribute as AliasAttribute;

            if (aliasAttr != null)
            {
                if (aliases == null)
                {
                    aliases = aliasAttr.aliasNames;
                }
                else
                {
                    var prevAliasNames = aliases;
                    var newAliasNames  = aliasAttr.aliasNames;
                    aliases = new string[prevAliasNames.Length + newAliasNames.Length];
                    Array.Copy(prevAliasNames, aliases, prevAliasNames.Length);
                    Array.Copy(newAliasNames, 0, aliases, prevAliasNames.Length, newAliasNames.Length);
                }

                return;
            }

            ArgumentTransformationAttribute argumentAttr = attribute as ArgumentTransformationAttribute;

            if (argumentAttr != null)
            {
                if (argTransformationAttributes == null)
                {
                    argTransformationAttributes = new Collection <ArgumentTransformationAttribute>();
                }
                argTransformationAttributes.Add(argumentAttr);
                return;
            }

            AllowNullAttribute allowNullAttribute = attribute as AllowNullAttribute;

            if (allowNullAttribute != null)
            {
                this.AllowsNullArgument = true;
                return;
            }

            AllowEmptyStringAttribute allowEmptyStringAttribute = attribute as AllowEmptyStringAttribute;

            if (allowEmptyStringAttribute != null)
            {
                this.AllowsEmptyStringArgument = true;
                return;
            }

            AllowEmptyCollectionAttribute allowEmptyCollectionAttribute = attribute as AllowEmptyCollectionAttribute;

            if (allowEmptyCollectionAttribute != null)
            {
                this.AllowsEmptyCollectionArgument = true;
                return;
            }

            ObsoleteAttribute obsoleteAttr = attribute as ObsoleteAttribute;

            if (obsoleteAttr != null)
            {
                ObsoleteAttribute = obsoleteAttr;
                return;
            }

            PSTypeNameAttribute psTypeNameAttribute = attribute as PSTypeNameAttribute;

            if (psTypeNameAttribute != null)
            {
                this.PSTypeName = psTypeNameAttribute.PSTypeName;
            }
        }