Example #1
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;
                         }
                     }
                 }
             }
         }
     }
 }
Example #2
0
        internal bool IsMatchingType(PSTypeName psTypeName)
        {
            Type fromType = psTypeName.Type;

            if (fromType != null)
            {
                bool flag = LanguagePrimitives.FigureConversion(typeof(object), this.ParameterType).Rank >= ConversionRank.AssignableS2A;
                if (fromType.Equals(typeof(object)))
                {
                    return(flag);
                }
                if (flag)
                {
                    return((psTypeName.Type != null) && psTypeName.Type.Equals(typeof(object)));
                }
                LanguagePrimitives.ConversionData data = LanguagePrimitives.FigureConversion(fromType, this.ParameterType);
                return((data != null) && (data.Rank >= ConversionRank.NumericImplicitS2A));
            }
            WildcardPattern pattern = new WildcardPattern("*" + (psTypeName.Name ?? ""), WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);

            if (pattern.IsMatch(this.ParameterType.FullName))
            {
                return(true);
            }
            if (this.ParameterType.IsArray && pattern.IsMatch(this.ParameterType.GetElementType().FullName))
            {
                return(true);
            }
            if (this.Attributes != null)
            {
                PSTypeNameAttribute attribute = this.Attributes.OfType <PSTypeNameAttribute>().FirstOrDefault <PSTypeNameAttribute>();
                if ((attribute != null) && pattern.IsMatch(attribute.PSTypeName))
                {
                    return(true);
                }
            }
            return(false);
        }
Example #3
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;
            }
        }
Example #4
0
        private string GetProxyAttributeData(Attribute attrib, string prefix)
        {
            ValidateLengthAttribute attribute = attrib as ValidateLengthAttribute;

            if (attribute != null)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateLength({1}, {2})]", new object[] { prefix, attribute.MinLength, attribute.MaxLength }));
            }
            ValidateRangeAttribute attribute2 = attrib as ValidateRangeAttribute;

            if (attribute2 != null)
            {
                string str2;
                Type   type = attribute2.MinRange.GetType();
                if ((type == typeof(float)) || (type == typeof(double)))
                {
                    str2 = "{0}[ValidateRange({1:R}, {2:R})]";
                }
                else
                {
                    str2 = "{0}[ValidateRange({1}, {2})]";
                }
                return(string.Format(CultureInfo.InvariantCulture, str2, new object[] { prefix, attribute2.MinRange, attribute2.MaxRange }));
            }
            if (attrib is AllowNullAttribute)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[AllowNull()]", new object[] { prefix }));
            }
            if (attrib is AllowEmptyStringAttribute)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[AllowEmptyString()]", new object[] { prefix }));
            }
            if (attrib is AllowEmptyCollectionAttribute)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[AllowEmptyCollection()]", new object[] { prefix }));
            }
            ValidatePatternAttribute attribute6 = attrib as ValidatePatternAttribute;

            if (attribute6 != null)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidatePattern('{1}')]", new object[] { prefix, CommandMetadata.EscapeSingleQuotedString(attribute6.RegexPattern) }));
            }
            ValidateCountAttribute attribute7 = attrib as ValidateCountAttribute;

            if (attribute7 != null)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateCount({1}, {2})]", new object[] { prefix, attribute7.MinLength, attribute7.MaxLength }));
            }
            if (attrib is ValidateNotNullAttribute)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateNotNull()]", new object[] { prefix }));
            }
            if (attrib is ValidateNotNullOrEmptyAttribute)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateNotNullOrEmpty()]", new object[] { prefix }));
            }
            ValidateSetAttribute attribute10 = attrib as ValidateSetAttribute;

            if (attribute10 != null)
            {
                StringBuilder builder = new StringBuilder();
                string        str3    = "";
                foreach (string str4 in attribute10.ValidValues)
                {
                    builder.AppendFormat(CultureInfo.InvariantCulture, "{0}'{1}'", new object[] { str3, CommandMetadata.EscapeSingleQuotedString(str4) });
                    str3 = ",";
                }
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateSet({1})]", new object[] { prefix, builder.ToString() }));
            }
            ValidateScriptAttribute attribute11 = attrib as ValidateScriptAttribute;

            if (attribute11 != null)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[ValidateScript({{ {1} }})]", new object[] { prefix, attribute11.ScriptBlock.ToString() }));
            }
            PSTypeNameAttribute attribute12 = attrib as PSTypeNameAttribute;

            if (attribute12 != null)
            {
                return(string.Format(CultureInfo.InvariantCulture, "{0}[PSTypeName('{1}')]", new object[] { prefix, CommandMetadata.EscapeSingleQuotedString(attribute12.PSTypeName) }));
            }
            return(null);
        }