Пример #1
0
        /// <summary>
        /// Determines if the value is valid for the specified attribute
        /// </summary>
        /// <param name="value">
        /// The variable value to validate.
        /// </param>
        /// <param name="attribute">
        /// The attribute to use to validate that value.
        /// </param>
        /// <returns>
        /// True if the value is valid with respect to the attribute, or false otherwise.
        /// </returns>
        internal static bool IsValidValue(object value, Attribute attribute)
        {
            bool result = true;

            ValidateArgumentsAttribute validationAttribute = attribute as ValidateArgumentsAttribute;

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

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

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

                    validationAttribute.InternalValidate(value, engine);
                }
                catch (ValidationMetadataException)
                {
                    result = false;
                }
            }
            return(result);
        } // IsValidValue
Пример #2
0
 public static void IsParameter(Expression<Func<object>> member, ParameterAttribute param, ValidateArgumentsAttribute validator)
 {
     AttributeAssert.Has(member, param);
     if (validator != null)
     {
         AttributeAssert.Has(member, validator);
     }
 }
Пример #3
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;
                         }
                     }
                 }
             }
         }
     }
 }
Пример #4
0
        internal static bool IsValidValue(object value, Attribute attribute)
        {
            bool flag = true;
            ValidateArgumentsAttribute attribute2 = attribute as ValidateArgumentsAttribute;

            if (attribute2 != null)
            {
                try
                {
                    ExecutionContext executionContextFromTLS = LocalPipeline.GetExecutionContextFromTLS();
                    EngineIntrinsics engineIntrinsics        = null;
                    if (executionContextFromTLS != null)
                    {
                        engineIntrinsics = executionContextFromTLS.EngineIntrinsics;
                    }
                    attribute2.InternalValidate(value, engineIntrinsics);
                }
                catch (ValidationMetadataException)
                {
                    flag = false;
                }
            }
            return(flag);
        }
Пример #5
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;
            }
        }
Пример #6
0
 public static void IsParameter(Expression<Func<object>> member, ValidateArgumentsAttribute validator)
 {
     IsParameter(member, new ParameterAttribute(), validator);
 }