public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue, PropertyDescriptor descriptor, bool ifExists) { var attr = descriptor.Property.GetCustomAttribute <SettingsAttribute>(); if (attr != null) { var behavior = new SettingsBehavior() { KeyPrefix = key, PrefixSeparator = this.PrefixSeparator //attr.PrefixSeparator ?? DEFAULT_PREFIX_SEPARATOR }; var desc = new PropertyDescriptor(new[] { behavior }); desc.AddBehavior(behavior); storedValue = dictionaryAdapter.This.Factory.GetAdapter(descriptor.PropertyType, dictionaryAdapter.This.Dictionary, desc); } if (ValueIsNullOrDefault(descriptor, storedValue)) { var defaultValue = descriptor.Annotations.OfType <DefaultValueAttribute>().SingleOrDefault(); if (IsRequired(descriptor, ifExists)) { throw new ValidationException(string.Format("No valid value for '{0}' found", key)); } else if (defaultValue != null) { storedValue = defaultValue.Value; } } // Convert value if needed. if (storedValue != null && !descriptor.PropertyType.IsAssignableFrom(storedValue.GetType())) { storedValue = descriptor.TypeConverter.CanConvertFrom(storedValue.GetType()) ? descriptor.TypeConverter.ConvertFrom(storedValue) : Convert.ChangeType(storedValue, descriptor.PropertyType); } #if !USE_DAVALIDATOR if (storedValue != null) { var propinfo = descriptor.Property; var context = new ValidationContext(storedValue) { DisplayName = propinfo.Name, MemberName = propinfo.Name }; { var attrs = propinfo.GetCustomAttributes(true).OfType <ValidationAttribute>().ToArray(); Validator.ValidateValue(storedValue, context, attrs); } } #endif return(storedValue); }
public T Create <T>(NameValueCollection nameValues) where T : class { var dict = CreateDictionary(nameValues); var meta = _factory.GetAdapterMeta(typeof(T)); var attr = typeof(T).GetCustomAttribute <SettingsAttribute>(); var behavior = new SettingsBehavior(attr?.KeyPrefix, attr?.PrefixSeparator); var desc = new PropertyDescriptor(new[] { behavior }); desc.AddBehavior(behavior); return((T)meta.CreateInstance(dict, desc)); }
public object GetPropertyValue(IDictionaryAdapter dictionaryAdapter, string key, object storedValue, PropertyDescriptor descriptor, bool ifExists) { var attr = descriptor.Property.GetCustomAttribute<SettingsAttribute>(); if (attr != null) { var behavior = new SettingsBehavior() { KeyPrefix = key, PrefixSeparator = this.PrefixSeparator //attr.PrefixSeparator ?? DEFAULT_PREFIX_SEPARATOR }; var desc = new PropertyDescriptor(new[] { behavior }); desc.AddBehavior(behavior); storedValue = dictionaryAdapter.This.Factory.GetAdapter(descriptor.PropertyType, dictionaryAdapter.This.Dictionary, desc); } if (ValueIsNullOrDefault(descriptor, storedValue)) { var defaultValue = descriptor.Annotations.OfType<DefaultValueAttribute>().SingleOrDefault(); if (IsRequired(descriptor, ifExists)) { throw new ValidationException(string.Format("No valid value for '{0}' found", key)); } else if (defaultValue != null) { storedValue = defaultValue.Value; } } // Convert value if needed. if (storedValue != null && !descriptor.PropertyType.IsAssignableFrom(storedValue.GetType())) { storedValue = descriptor.TypeConverter.CanConvertFrom(storedValue.GetType()) ? descriptor.TypeConverter.ConvertFrom(storedValue) : Convert.ChangeType(storedValue, descriptor.PropertyType); } #if !USE_DAVALIDATOR if (storedValue != null) { var propinfo = descriptor.Property; var context = new ValidationContext(storedValue) { DisplayName = propinfo.Name, MemberName = propinfo.Name }; { var attrs = propinfo.GetCustomAttributes(true).OfType<ValidationAttribute>().ToArray(); Validator.ValidateValue(storedValue, context, attrs); } } #endif return storedValue; }