Ejemplo n.º 1
0
        public string ParseAndValidateScopeValue(string scopeName, object value)
        {
            this.ValidateScopeName(scopeName);
            ConfigurationProperty property = this.ScopeSchema.GetConfigurationProperty(scopeName, null);

            ExchangeConfigurationSection.RunConfigOperation(delegate
            {
                if (property.Type != value.GetType())
                {
                    property.Converter.ConvertFrom(value);
                }
                property.Validator.Validate(value);
            }, (Exception ex) => new ConfigurationSettingsScopePropertyFailedValidationException(scopeName, (value != null) ? value.ToString() : null, ex));
            if (value == null)
            {
                return(null);
            }
            if (value is string)
            {
                return((string)value);
            }
            string result;

            try
            {
                result = property.Converter.ConvertToInvariantString(value);
            }
            catch (NotSupportedException innerException)
            {
                throw new ConfigurationSettingsScopePropertyBadValueException(scopeName, (value != null) ? value.ToString() : null, innerException);
            }
            return(result);
        }
Ejemplo n.º 2
0
        public object ParseAndValidateConfigValue(string settingName, string serializedValue, Type settingType = null)
        {
            ConfigurationProperty property = base.GetConfigurationProperty(settingName, settingType);
            object convertedValue          = null;

            ExchangeConfigurationSection.RunConfigOperation(delegate
            {
                TypeConverter converter = property.Converter;
                convertedValue          = converter.ConvertFromInvariantString(serializedValue);
            }, (Exception ex) => new ConfigurationSettingsPropertyBadValueException(settingName, serializedValue, ex));
            this.ValidateConfigValue(property, convertedValue);
            return(convertedValue);
        }
Ejemplo n.º 3
0
 private void ValidateConfigValue(ConfigurationProperty property, object value)
 {
     if (property == null)
     {
         throw new ArgumentNullException("property");
     }
     ExchangeConfigurationSection.RunConfigOperation(delegate
     {
         ConfigurationValidatorBase validator = property.Validator;
         if (validator != null)
         {
             validator.Validate(value);
         }
     }, (Exception ex) => new ConfigurationSettingsPropertyFailedValidationException(property.Name, (value != null) ? value.ToString() : null, ex));
 }
Ejemplo n.º 4
0
        public static T ConvertValue <T>(IConfigSchema schema, string settingName, object rawValue)
        {
            if (!(rawValue is T))
            {
                ConfigurationProperty property = schema.GetConfigurationProperty(settingName, typeof(T));
                ExchangeConfigurationSection.RunConfigOperation(delegate
                {
                    TypeConverter converter = property.Converter;
                    rawValue = converter.ConvertTo(rawValue, typeof(T));
                }, (Exception ex) => new ConfigurationSettingsPropertyBadTypeException(string.Format("{0}:{1}", settingName, (rawValue != null) ? rawValue.GetType().ToString() : "(null)"), typeof(T).ToString(), ex));
            }
            T result;

            try
            {
                result = (T)((object)rawValue);
            }
            catch (InvalidCastException innerException)
            {
                throw new ConfigurationSettingsPropertyBadTypeException(string.Format("{0}:{1}", settingName, (rawValue != null) ? rawValue.GetType().ToString() : "(null)"), typeof(T).ToString(), innerException);
            }
            return(result);
        }