Esempio n. 1
0
 internal SettingsBuilder(SettingsOptions options,
                          IEnumerable <ISectionBinder> sectionBinders)
     : this(options,
            sectionBinders,
            new SettingsTypesExtractor(),
            new SettingsOptionsValidator(),
            new SettingsClassGenerator(),
            new ValuesPopulator())
 {
 }
Esempio n. 2
0
 internal SettingsBuilder(SettingsOptions options,
                          IEnumerable <ISectionBinder> sectionBinders,
                          ISettingsTypesExtractor settingsTypesExtractor,
                          ISettingsOptionsValidator settingsOptionsValidator,
                          ISettingsClassGenerator settingsClassGenerator,
                          IValuesPopulator valuesPopulator)
 {
     Options                   = options;
     SectionBinders            = sectionBinders;
     _settingsTypesExtractor   = settingsTypesExtractor;
     _settingsOptionsValidator = settingsOptionsValidator;
     _settingsClassGenerator   = settingsClassGenerator;
     _valuesPopulator          = valuesPopulator;
 }
Esempio n. 3
0
        private object ConvertPropertyValue(Type settingsType,
                                            object value,
                                            PropertyInfo property,
                                            SettingsOptions options)
        {
            try
            {
                var propertyValue = _typeConverter.ConvertValue(value, property, options);

                return(propertyValue);
            }
            catch (Exception e)
            {
                throw new SettingsPropertyValueException(settingsType, value, property, e);
            }
        }
Esempio n. 4
0
        public void PopulateInstanceWithValues(object instance,
                                               Type settings,
                                               SettingsOptions options,
                                               IEnumerable <ISectionBinder> binders)
        {
            var sectionBinders = binders as ISectionBinder[] ?? binders.ToArray();

            foreach (var property in _typePropertiesExtractor.ExtractTypeProperties(settings))
            {
                var tempValue = property.GetDefaultValue();
                foreach (var binder in sectionBinders)
                {
                    var context = new BindingContext(settings.GetSectionName(options),
                                                     property.GetPropertyName(),
                                                     settings,
                                                     property,
                                                     tempValue);

                    try
                    {
                        binder.BindPropertySettings(context);
                        if (context.HasNewValue)
                        {
                            tempValue = context.NewValue;
                        }
                    }
                    catch (Exception e)
                    {
                        throw new SettingsBindingException(binder, context, e);
                    }
                }

                var propertyValue = ConvertPropertyValue(settings, tempValue, property, options);
                property.SetValue(instance, propertyValue);
            }
        }