Esempio n. 1
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);
        }
        /// <summary>
        /// This function is part of the IDynamicParameters interface.
        /// PowerShell uses it to generate parameters dynamically.
        /// We have to generate -ResourceType parameter dynamically because the array
        /// of resources that we used to validate against are not generated before compile time,
        /// i.e. [ValidateSet(ArrayGeneratedAtRunTime)] will throw an error for parameters
        /// that are not generated dynamically.
        /// </summary>
        public object GetDynamicParameters()
        {
            if (_dynamicParameters == null)
            {
                ParameterAttribute paramAttribute = new ParameterAttribute()
                {
                    Mandatory = true
                };
                ValidateSetAttribute validateSetAttribute = new ValidateSetAttribute(AllResourceTypes);
                validateSetAttribute.IgnoreCase = true;
                Collection<Attribute> attributes =
                    new Collection<Attribute>(new Attribute[] { validateSetAttribute, paramAttribute });
                // This parameter can now be thought of as:
                // [Parameter(Mandatory = true)]
                // [ValidateSet(validTypeValues)]
                // public string { get; set; }
                RuntimeDefinedParameter typeParameter = new RuntimeDefinedParameter("ResourceType", typeof(string), attributes);
                _dynamicParameters = new RuntimeDefinedParameterDictionary();
                _dynamicParameters.Add("ResourceType", typeParameter);
            }

            return _dynamicParameters;
        }