fixed IsDirty Property, added tests for IsDirty Property
[mono.git] / mcs / class / System / System.Configuration / SettingsPropertyValue.cs
index ab77c5e34fedba2626cf5ad578b5db21dc70ee94..525d2b1e156d551b2ecfb446623a7737fb73f71f 100644 (file)
@@ -80,29 +80,19 @@ namespace System.Configuration
                public object PropertyValue {
                        get {
                                if (needPropertyValue) {
-                                       propertyValue = GetDeserializedValue ();
-                                       if (propertyValue == null) {\r
-                                               if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {\r
-                                                       TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);\r
-                                                       propertyValue = converter.ConvertFrom (property.DefaultValue);\r
-                                               }\r
-                                               else {\r
-                                                       propertyValue = property.DefaultValue;\r
-                                               }
+                                       propertyValue = GetDeserializedValue (serializedValue);
+                                       if (propertyValue == null) {
+                                               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;
                        }
@@ -163,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;
@@ -173,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)
@@ -198,7 +212,7 @@ namespace System.Configuration
                        return deserializedObject;
                }
 
-               SettingsProperty property;
+               readonly SettingsProperty property;
                object propertyValue;
                object serializedValue;
                bool needSerializedValue;