static void SetValue <T>(T obj, MemberInfo member, CommanderAttribute param, string name, string value)
        {
            if (param.Regex != null)
            {
                // We need to validate this value with a regex
                if (!value.Matches(param.Regex))
                {
                    throw new ParameterMatchException(param, value);
                }
            }

            if (param.ValidateWith != null)
            {
                // We need to validate this value with a validator
                if (!typeof(IParameterValidator).GetTypeInfo().IsAssignableFrom(param.ValidateWith))
                {
                    throw new ValidatorTypeException(param.ValidateWith);
                }

                IParameterValidator validator = (IParameterValidator)Activator.CreateInstance(param.ValidateWith);

                if (!validator.Validate(name, value))
                {
                    throw new ParameterValidationException(validator);
                }
            }

            object convertedValue;

            if (param.FormatWith != null)
            {
                // We need to format this value
                if (!typeof(IParameterFormatter).GetTypeInfo().IsAssignableFrom(param.FormatWith))
                {
                    throw new FormatterTypeException(param.FormatWith);
                }

                IParameterFormatter formatter = (IParameterFormatter)Activator.CreateInstance(param.FormatWith);

                convertedValue = formatter.Format(name, value);
            }
            else
            {
                // We need to try and convert the value ourselves
                try
                {
                    convertedValue = ValueParse(member.Type(), value);
                }
                catch (FormatException)
                {
                    // The value had to be parsed to a numerical type and failed
                    throw new ParameterFormatException(param, value, member.GetType());
                }
            }

            SetValue(obj, member, convertedValue);
        }
 internal ParameterException(CommanderAttribute attrib)
 {
     this.attrib = attrib;
 }
Example #3
0
 internal ParameterFormatException(CommanderAttribute attrib, string value, Type requiredType) : base(attrib)
 {
     Value        = value;
     RequiredType = requiredType;
 }
 static bool ParamRequired <T>(T defaultObj, CommanderAttribute param, MemberInfo member)
 {
     return(param.Required == Required.Yes ||
            (param.Required == Required.Default && GetDefaultValue <T>(defaultObj, member) == null));
 }
 internal ParameterMissingException(CommanderAttribute attrib) : base(attrib)
 {
 }
Example #6
0
 internal ParameterMatchException(CommanderAttribute attrib, string value) : base(attrib)
 {
     Value = value;
 }