fixed IsDirty Property, added tests for IsDirty Property
[mono.git] / mcs / class / System / System.Configuration / SettingsPropertyValue.cs
index db1e781159289d0666f99395bf8b3da149b1ae4d..525d2b1e156d551b2ecfb446623a7737fb73f71f 100644 (file)
 #if NET_2_0
 using System;
 using System.IO;
+using System.ComponentModel;
 using System.Runtime.Serialization.Formatters.Binary;
 #if (XML_DEP)
+using System.Xml;
 using System.Xml.Serialization;
 #endif
 
@@ -78,20 +80,19 @@ namespace System.Configuration
                public object PropertyValue {
                        get {
                                if (needPropertyValue) {
+                                       propertyValue = GetDeserializedValue (serializedValue);
+                                       if (propertyValue == null) {
+                                               propertyValue = GetDeserializedDefaultValue ();
+                                               defaulted = true;
+                                       }
                                        needPropertyValue = false;
-                                       propertyValue = property.DefaultValue;
-                                       defaulted = true;
                                }
 
-#if notyet
-                               /* LAMESPEC: the msdn2 docs say that
-                                * for object types this
-                                * pessimistically sets Dirty == true.
-                                * tests, however, point out that that
-                                * is not the case. */
-                               if (!property.PropertyType.IsValueType)
+                               if (propertyValue != null &&
+                                       !(propertyValue is string) &&
+                                       !(propertyValue is DateTime) &&
+                                       !property.PropertyType.IsPrimitive)
                                        dirty = true;
-#endif
 
                                return propertyValue;
                        }
@@ -142,6 +143,7 @@ namespace System.Configuration
                        }
                        set {
                                serializedValue = value;
+                               needPropertyValue = true;
                        }
                }
 
@@ -151,13 +153,72 @@ namespace System.Configuration
                        }
                }
 
-               SettingsProperty property;
+               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 (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 && ((string) serializedValue).Length > 0)
+                                                       deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromString ((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 = new MemoryStream ((byte []) serializedValue);
+                                               deserializedObject = bf.Deserialize (ms);
+                                               break;
+                               }
+                       }
+                       catch (Exception e) {
+                               if (property.ThrowOnErrorDeserializing)
+                                       throw e;
+                       }
+
+                       return deserializedObject;
+               }
+
+               readonly SettingsProperty property;
                object propertyValue;
                object serializedValue;
                bool needSerializedValue;
                bool needPropertyValue;
                bool dirty;
-               bool defaulted;
+               bool defaulted = false;
                bool deserialized;
        }