<PackageReference Include="System.Configuration.ConfigurationManager" Version="8.0.0-preview.4.23259.5" />

SettingsPropertyValue

public class SettingsPropertyValue
Contains the value of a settings property that can be loaded and stored by an instance of SettingsBase.
using System.ComponentModel; using System.Globalization; using System.IO; using System.Reflection; using System.Runtime.Serialization.Formatters.Binary; using System.Xml.Serialization; namespace System.Configuration { public class SettingsPropertyValue { private object _value; private object _serializedValue; private bool _changedSinceLastSerialized; public string Name => Property.Name; public bool IsDirty { get; set; } public SettingsProperty Property { get; set; } public bool UsingDefaultValue { get; set; } public bool Deserialized { get; set; } public object PropertyValue { get { if (!Deserialized) { _value = Deserialize(); Deserialized = true; } if (_value != null && !Property.PropertyType.IsPrimitive && !(_value is string) && !(_value is DateTime)) { UsingDefaultValue = false; _changedSinceLastSerialized = true; IsDirty = true; } return _value; } set { _value = value; IsDirty = true; _changedSinceLastSerialized = true; Deserialized = true; UsingDefaultValue = false; } } public object SerializedValue { get { if (_changedSinceLastSerialized) { _changedSinceLastSerialized = false; _serializedValue = SerializePropertyValue(); } return _serializedValue; } set { UsingDefaultValue = false; _serializedValue = value; } } public SettingsPropertyValue(SettingsProperty property) { Property = property; } private static bool IsHostedInAspnet() { return AppDomain.CurrentDomain.GetData(".appDomain") != null; } private object Deserialize() { object obj = null; if (SerializedValue != null) { bool flag = false; try { if (SerializedValue is string) obj = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)SerializedValue); else if (SettingsProperty.EnableUnsafeBinaryFormatterInPropertyValueSerialization) { using (MemoryStream serializationStream = new MemoryStream((byte[])SerializedValue)) obj = new BinaryFormatter().Deserialize(serializationStream); } else { flag = true; } } catch (Exception ex) { try { if (IsHostedInAspnet()) { object[] args = new object[3] { Property, this, ex }; Type type = Type.GetType("System.Web.Management.WebBaseEvent, System.Web", true); type.InvokeMember("RaisePropertyDeserializationWebErrorEvent", BindingFlags.Static | BindingFlags.NonPublic | BindingFlags.InvokeMethod, null, null, args, CultureInfo.InvariantCulture); } } catch { } } if (flag) throw new NotSupportedException("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information."); if (obj != null && !Property.PropertyType.IsAssignableFrom(obj.GetType())) obj = null; } if (obj == null) { UsingDefaultValue = true; if (Property.DefaultValue == null || Property.DefaultValue.ToString() == "[null]") { if (Property.PropertyType.IsValueType) return TypeUtil.CreateInstance(Property.PropertyType); return null; } if (Property.DefaultValue is string) try { obj = GetObjectFromString(Property.PropertyType, Property.SerializeAs, (string)Property.DefaultValue); } catch (Exception ex2) { throw new ArgumentException(System.SR.Format(System.SR.Could_not_create_from_default_value, Property.Name, ex2.Message)); } else obj = Property.DefaultValue; if (obj != null && !Property.PropertyType.IsAssignableFrom(obj.GetType())) throw new ArgumentException(System.SR.Format(System.SR.Could_not_create_from_default_value_2, Property.Name)); } if (obj == null) { if (!(Property.PropertyType == typeof(string))) try { obj = TypeUtil.CreateInstance(Property.PropertyType); return obj; } catch { return obj; } obj = string.Empty; } return obj; } private static object GetObjectFromString(Type type, SettingsSerializeAs serializeAs, string serializedValue) { if (type == typeof(string) && (serializedValue == null || serializedValue.Length < 1 || serializeAs == SettingsSerializeAs.String)) return serializedValue; if (serializedValue != null && serializedValue.Length >= 1) { switch (serializeAs) { case SettingsSerializeAs.Binary: if (SettingsProperty.EnableUnsafeBinaryFormatterInPropertyValueSerialization) { byte[] buffer = Convert.FromBase64String(serializedValue); using (MemoryStream serializationStream = new MemoryStream(buffer)) return new BinaryFormatter().Deserialize(serializationStream); } throw new NotSupportedException("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information."); case SettingsSerializeAs.Xml: { StringReader textReader = new StringReader(serializedValue); XmlSerializer xmlSerializer = new XmlSerializer(type); return xmlSerializer.Deserialize(textReader); } case SettingsSerializeAs.String: { TypeConverter converter = TypeDescriptor.GetConverter(type); if (converter != null && converter.CanConvertTo(typeof(string)) && converter.CanConvertFrom(typeof(string))) return converter.ConvertFromInvariantString(serializedValue); throw new ArgumentException(System.SR.Format(System.SR.Unable_to_convert_type_from_string, type), "type"); } default: return null; } } return null; } private object SerializePropertyValue() { if (_value == null) return null; if (Property.SerializeAs != SettingsSerializeAs.Binary) return ConvertObjectToString(_value, Property.PropertyType, Property.SerializeAs, Property.ThrowOnErrorSerializing); if (SettingsProperty.EnableUnsafeBinaryFormatterInPropertyValueSerialization) { using (MemoryStream memoryStream = new MemoryStream()) { BinaryFormatter binaryFormatter = new BinaryFormatter(); binaryFormatter.Serialize(memoryStream, _value); return memoryStream.ToArray(); } } throw new NotSupportedException("BinaryFormatter serialization is obsolete and should not be used. See https://aka.ms/binaryformatter for more information."); } private static string ConvertObjectToString(object propertyValue, Type type, SettingsSerializeAs serializeAs, bool throwOnError) { if (serializeAs == SettingsSerializeAs.ProviderSpecific) serializeAs = ((!(type == typeof(string)) && !type.IsPrimitive) ? SettingsSerializeAs.Xml : SettingsSerializeAs.String); try { switch (serializeAs) { case SettingsSerializeAs.Binary: break; case SettingsSerializeAs.String: { TypeConverter converter = TypeDescriptor.GetConverter(type); if (converter == null || !converter.CanConvertTo(typeof(string)) || !converter.CanConvertFrom(typeof(string))) throw new ArgumentException(System.SR.Format(System.SR.Unable_to_convert_type_to_string, type), "type"); return converter.ConvertToInvariantString(propertyValue); } case SettingsSerializeAs.Xml: { XmlSerializer xmlSerializer = new XmlSerializer(type); StringWriter stringWriter = new StringWriter(CultureInfo.InvariantCulture); xmlSerializer.Serialize(stringWriter, propertyValue); return stringWriter.ToString(); } } } catch (Exception) { if (throwOnError) throw; } return null; } } }