コード例 #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);
        }
コード例 #2
0
        public override XElement GetDiagnosticInfo(string argument)
        {
            XElement diagnosticInfo = base.GetDiagnosticInfo(argument);

            diagnosticInfo.Add(new XElement("description", "App config contains DEFAULTS.  They may be overrided by updating app.config directly but it's not recommended"));
            ConfigurationSection configurationSection = this.Section;

            if (configurationSection != null)
            {
                XElement xelement = new XElement(base.Schema.SectionName);
                ExchangeConfigurationSection exchangeConfigurationSection = configurationSection as ExchangeConfigurationSection;
                if (exchangeConfigurationSection != null)
                {
                    using (IEnumerator <string> enumerator = exchangeConfigurationSection.Settings.GetEnumerator())
                    {
                        while (enumerator.MoveNext())
                        {
                            string text = enumerator.Current;
                            xelement.Add(new XElement(text, exchangeConfigurationSection.GetPropertyValue(text)));
                        }
                        goto IL_FC;
                    }
                }
                AppSettingsSection appSettingsSection = configurationSection as AppSettingsSection;
                foreach (object obj in appSettingsSection.Settings)
                {
                    KeyValueConfigurationElement keyValueConfigurationElement = (KeyValueConfigurationElement)obj;
                    xelement.Add(new XElement(keyValueConfigurationElement.Key, keyValueConfigurationElement.Value));
                }
IL_FC:
                diagnosticInfo.Add(xelement);
            }
            return(diagnosticInfo);
        }
コード例 #3
0
        public override bool TryGetBoxedSetting(ISettingsContext context, string settingName, Type settingType, out object settingValue)
        {
            ConfigurationSection configurationSection = this.Section;

            if (configurationSection != null)
            {
                ExchangeConfigurationSection exchangeConfigurationSection = configurationSection as ExchangeConfigurationSection;
                if (exchangeConfigurationSection != null)
                {
                    object propertyValue = exchangeConfigurationSection.GetPropertyValue(settingName);
                    if (propertyValue != exchangeConfigurationSection.GetConfigurationProperty(settingName, null).DefaultValue)
                    {
                        settingValue = propertyValue;
                        return(true);
                    }
                }
                else
                {
                    AppSettingsSection           appSettingsSection           = configurationSection as AppSettingsSection;
                    KeyValueConfigurationElement keyValueConfigurationElement = appSettingsSection.Settings[settingName];
                    if (keyValueConfigurationElement != null)
                    {
                        settingValue = base.ParseAndValidateConfigValue(settingName, keyValueConfigurationElement.Value, settingType);
                        return(true);
                    }
                }
            }
            settingValue = null;
            return(false);
        }
コード例 #4
0
 public static void RunConfigOperation(Action configOperation, Func <Exception, ConfigurationSettingsException> errorHandler)
 {
     ExchangeConfigurationSection.InternalRunConfigOperation(configOperation, delegate(Exception ex)
     {
         throw errorHandler(ex);
     });
 }
コード例 #5
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);
        }
コード例 #6
0
        public static bool TryConvertFromInvariantString(ConfigurationProperty configProperty, string toConvert, out object converted)
        {
            object convertedValue = null;

            ExchangeConfigurationSection.InternalRunConfigOperation(delegate
            {
                convertedValue = configProperty.Converter.ConvertFromInvariantString(toConvert);
            }, delegate(Exception ex)
            {
                convertedValue = null;
            });
            converted = convertedValue;
            return(converted != null);
        }
コード例 #7
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));
 }
コード例 #8
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);
        }