Пример #1
0
        /// <summary>
        /// Sets the value of a configuration.
        /// </summary>
        /// <param name="name">The configuration value to set.</param>
        /// <param name="value">The value to assign to the configuration.</param>
        private void SetValue(ConfigurationNames name, object value)
        {
            ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(name);

            if (attribute == null)
            {
                return;
            }

            ConfigurationValidation validation = attribute.Validation;

            if (validation != ConfigurationValidation.None)
            {
                if (validation.HasFlag(ConfigurationValidation.NonNull) && value == null)
                {
                    throw new ArgumentNullException("value");
                }

                if (validation.HasFlag(ConfigurationValidation.NonZero) ||
                    validation.HasFlag(ConfigurationValidation.NonNegative) ||
                    validation.HasFlag(ConfigurationValidation.PortRange))
                {
                    int converted = Convert.ToInt32(value);

                    if (validation.HasFlag(ConfigurationValidation.NonZero) && converted == 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.NonNegative) && converted < 0)
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration cannot be below zero");
                    }
                    if (validation.HasFlag(ConfigurationValidation.PortRange) && (converted <= 0 || short.MaxValue < converted))
                    {
                        throw new ArgumentOutOfRangeException("value", "The value of this configuration must be within a valid port range");
                    }
                }

                if (validation.HasFlag(ConfigurationValidation.SizeDivision))
                {
                    double converted = Convert.ToDouble(value);

                    if (converted % 1024 != 0)
                    {
                        throw new ArgumentException("The value of this configuration must be divisible by 1024");
                    }
                }
            }

            values[name] = value;

            if (PropertyChanged != null)
            {
                PropertyChanged(client, new PropertyChangedEventArgs(attribute.Name));
            }

            SetConfCommand command  = new SetConfCommand(attribute.Name, ReflectionHelper.Convert(value, typeof(string)) as string);
            Response       response = command.Dispatch(client);
        }
Пример #2
0
        /// <summary>
        /// Sets the value of all configurations to their default values.
        /// </summary>
        private void SetDefaults()
        {
            ConfigurationNames[] names = (ConfigurationNames[])Enum.GetValues(typeof(ConfigurationNames));

            foreach (ConfigurationNames name in names)
            {
                ConfigurationAssocAttribute attribute = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(name);

                if (attribute == null)
                {
                    continue;
                }

                values[name] = ReflectionHelper.Convert(attribute.Default, attribute.Type);
            }
        }
Пример #3
0
        /// <summary>
        /// Sets the value of a configuration directly, without raising any <c>setconf</c> commands.
        /// </summary>
        /// <param name="name">The name of the configuration value to set.</param>
        /// <param name="value">The value to assign to the configuration.</param>
        internal void SetValueDirect(string name, string value)
        {
            if (name == null)
            {
                throw new ArgumentNullException("name");
            }

            ConfigurationNames          association = ReflectionHelper.GetEnumerator <ConfigurationNames, ConfigurationAssocAttribute>(attr => attr.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase));
            ConfigurationAssocAttribute attribute   = ReflectionHelper.GetAttribute <ConfigurationNames, ConfigurationAssocAttribute>(association);

            if (attribute.Name.Equals(name, StringComparison.CurrentCultureIgnoreCase))
            {
                values[association] = ReflectionHelper.Convert(value, attribute.Type);

                if (PropertyChanged != null)
                {
                    PropertyChanged(client, new PropertyChangedEventArgs(attribute.Name));
                }
            }
        }