fixed IsDirty Property, added tests for IsDirty Property
[mono.git] / mcs / class / System / System.Configuration / SettingsPropertyValue.cs
index b5e80da3c25ae944bbf891f4ed48c3bdf990cd52..525d2b1e156d551b2ecfb446623a7737fb73f71f 100644 (file)
@@ -80,23 +80,19 @@ namespace System.Configuration
                public object PropertyValue {
                        get {
                                if (needPropertyValue) {
-                                       propertyValue = GetDeserializedValue ();
+                                       propertyValue = GetDeserializedValue (serializedValue);
                                        if (propertyValue == null) {
-                                               propertyValue = property.DefaultValue;
+                                               propertyValue = GetDeserializedDefaultValue ();
                                                defaulted = true;
                                        }
                                        needPropertyValue = false;
                                }
 
-#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;
                        }
@@ -157,7 +153,31 @@ namespace System.Configuration
                        }
                }
 
-               private object GetDeserializedValue ()
+               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;
@@ -167,7 +187,7 @@ namespace System.Configuration
                        try {
                                switch (property.SerializeAs) {
                                        case SettingsSerializeAs.String:
-                                               if (((string) serializedValue).Length > 0)
+                                               if (serializedValue is string && ((string) serializedValue).Length > 0)
                                                        deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromString ((string) serializedValue);
                                                break;
 #if (XML_DEP)
@@ -192,7 +212,7 @@ namespace System.Configuration
                        return deserializedObject;
                }
 
-               SettingsProperty property;
+               readonly SettingsProperty property;
                object propertyValue;
                object serializedValue;
                bool needSerializedValue;