+ internal object Reset ()
+ {
+ propertyValue = GetDeserializedDefaultValue ();
+ dirty = true;
+ defaulted = true;
+ needPropertyValue = true;
+ return propertyValue;
+ }
+
+ private object GetDeserializedDefaultValue ()
+ {
+ if (property.DefaultValue == null)
+ if (property.PropertyType.IsValueType)
+ return Activator.CreateInstance (property.PropertyType);
+ else
+ return null;
+
+ if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
+ if (property.PropertyType != typeof (string))
+ return Activator.CreateInstance (property.PropertyType);
+ else
+ return string.Empty;
+
+ if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
+ return GetDeserializedValue (property.DefaultValue);
+
+ if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
+ TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
+ return converter.ConvertFrom (null, CultureInfo.InvariantCulture, property.DefaultValue);
+ }
+ return property.DefaultValue;
+ }
+
+ private object GetDeserializedValue (object serializedValue)
+ {
+ if (serializedValue == null)
+ return null;
+
+ object deserializedObject = null;
+
+ try {
+ switch (property.SerializeAs) {
+ case SettingsSerializeAs.String:
+ if (serializedValue is string)
+ deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromInvariantString ((string) serializedValue);
+ break;
+#if (XML_DEP)
+ case SettingsSerializeAs.Xml:
+ XmlSerializer serializer = new XmlSerializer (property.PropertyType);
+ StringReader str = new StringReader ((string) serializedValue);
+ deserializedObject = serializer.Deserialize (XmlReader.Create (str));
+ break;
+#endif
+ case SettingsSerializeAs.Binary:
+ BinaryFormatter bf = new BinaryFormatter ();
+ MemoryStream ms;
+ if (serializedValue is string)
+ ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
+ else
+ ms = new MemoryStream ((byte []) serializedValue);
+ deserializedObject = bf.Deserialize (ms);
+ break;
+ }
+ }
+ catch (Exception e) {
+ if (property.ThrowOnErrorDeserializing)
+ throw e;
+ }
+
+ return deserializedObject;
+ }
+
+ readonly SettingsProperty property;