Ejemplo n.º 1
0
        private static void SetValue(object argumentObject, ArgumentParameterAttribute attr, string argValue)
        {
            string[] values = null;
            if (attr.MaxOccur > 1)
            {
                values = argValue.Split(new char[] { attr.Delimiter }, StringSplitOptions.RemoveEmptyEntries);
            }
            else
            {
                if (string.IsNullOrEmpty(argValue) && attr.PropertyInfo.PropertyType.GetBaseType() != typeof(bool))
                {
                    values = new string[0];
                }
                else
                {
                    values = new string[1] {
                        argValue
                    };
                }
            }

            if (values.Length < attr.MinOccur)
            {
                throw new ArgumentException(attr.ParameterName + "'s MinOccur=" + attr.MinOccur +
                                            " but argument only has " + values.Length + " occurs");
            }

            // regex validation
            if (attr.ValidationRegex != null)
            {
                foreach (string v in values)
                {
                    if (!attr.ValidationRegex.IsMatch(v))
                    {
                        throw new ArgumentException(attr.ParameterName + " has regex restriction, but value:" + v +
                                                    " does not match regex:" + attr.ValidationRegex.ToString());
                    }
                }
            }

            SetValueFromStrings(argumentObject, attr.PropertyInfo, values);
            attr.SetCount += values.Length;
        }
Ejemplo n.º 2
0
        private string GetArgumentParameterUsage(ArgumentParameterAttribute attr)
        {
            string msg = string.Format("    parameter: {0}  [{1}..{2}, {3}]  ({4})",
                                       attr.ParameterName,
                                       attr.MinOccur,
                                       attr.MaxOccur,
                                       attr.PropertyInfo.PropertyType.GetBaseType().Name,
                                       attr.Description);

            if (attr.DefaultValue != null && attr.MaxOccur <= 1)
            {
                msg += "\r\n      default value: " + attr.DefaultValue.ToString();
            }
            if (attr.Delimiter != default(byte) && attr.MaxOccur > 1)
            {
                msg += "\r\n      delimiter: '" + attr.Delimiter.ToString() + "'";
            }
            if (attr.ValidationRegex != null)
            {
                msg += "\r\n      regex: " + attr.ValidationRegex.ToString();
            }
            return(msg);
        }
Ejemplo n.º 3
0
        private static void ValidateParameterOptionAttr(
            PropertyInfo property,
            ArgumentParameterAttribute attr,
            IDictionary <string, string> options)
        {
            try
            {
                attr.Validate();
            }
            catch (ArgumentException ae)
            {
                throw new ArgumentException(property.Name + " has invalid ParameterOptionAttribute, message:",
                                            ae.Message);
            }

            foreach (string option in attr.OptionsBindTo)
            {
                if (!options.ContainsKey(option))
                {
                    throw new ArgumentException(option + " is not defined in ArgumentObject");
                }
            }

            if (attr.MaxOccur > 1 && !property.PropertyType.IsArray)
            {
                throw new ArgumentException(property.Name + " is not array type but MaxOccurs > 1");
            }

            if (attr.DefaultValue != null && attr.DefaultValue.GetType() != property.PropertyType)
            {
                throw new ArgumentException(string.Format("DefaultValue is set, type:{0} != propertyType:{1}",
                                                          attr.DefaultValue.GetType().Name,
                                                          property.PropertyType.Name));
            }
            attr.PropertyInfo = property;
        }