* SettingsPropertyValue.cs: fixed PropertyValue property when it should be deserialized
authorVladimir Krasnov <krasnov@mono-cvs.ximian.com>
Sun, 5 Nov 2006 11:53:45 +0000 (11:53 -0000)
committerVladimir Krasnov <krasnov@mono-cvs.ximian.com>
Sun, 5 Nov 2006 11:53:45 +0000 (11:53 -0000)
* SettingsPropertyValueTest.cs: added tests of value deserealization

svn path=/trunk/mcs/; revision=67369

mcs/class/System/System.Configuration/ChangeLog
mcs/class/System/System.Configuration/SettingsPropertyValue.cs
mcs/class/System/Test/System.Configuration/ChangeLog
mcs/class/System/Test/System.Configuration/SettingsPropertyValueTest.cs

index 1464a060c8846f866410143ab6a79a12b705492a..e31d052d1d7505524edbb70a8a92d5fe476764e5 100644 (file)
@@ -1,3 +1,8 @@
+2006-11-05  Vladimir Krasnov  <vladimirk@mainsoft.com>
+
+       * SettingsPropertyValue.cs: fixed PropertyValue property when it
+       should be deserialized.
+
 2006-10-13  Atsushi Enomoto  <atsushi@ximian.com>
 
        * ConfigurationSettings.cs :
index db1e781159289d0666f99395bf8b3da149b1ae4d..b5e80da3c25ae944bbf891f4ed48c3bdf990cd52 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,9 +80,12 @@ namespace System.Configuration
                public object PropertyValue {
                        get {
                                if (needPropertyValue) {
+                                       propertyValue = GetDeserializedValue ();
+                                       if (propertyValue == null) {
+                                               propertyValue = property.DefaultValue;
+                                               defaulted = true;
+                                       }
                                        needPropertyValue = false;
-                                       propertyValue = property.DefaultValue;
-                                       defaulted = true;
                                }
 
 #if notyet
@@ -142,6 +147,7 @@ namespace System.Configuration
                        }
                        set {
                                serializedValue = value;
+                               needPropertyValue = true;
                        }
                }
 
@@ -151,13 +157,48 @@ namespace System.Configuration
                        }
                }
 
+               private object GetDeserializedValue ()
+               {
+                       if (serializedValue == null)
+                               return null;
+
+                       object deserializedObject = null;
+
+                       try {
+                               switch (property.SerializeAs) {
+                                       case SettingsSerializeAs.String:
+                                               if (((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;
+               }
+
                SettingsProperty property;
                object propertyValue;
                object serializedValue;
                bool needSerializedValue;
                bool needPropertyValue;
                bool dirty;
-               bool defaulted;
+               bool defaulted = false;
                bool deserialized;
        }
 
index ae09956394f1809ca21ce2f9c4a484f591942652..7dfe7231967352be40ca39d5f9d8eea42764c65e 100644 (file)
@@ -1,3 +1,7 @@
+2006-11-05  Vladimir Krasnov  <vladimirk@mainsoft.com>
+
+       * SettingsPropertyValueTest.cs: added tests of value deserealization
+
 2006-06-29  Atsushi Enomoto  <atsushi@ximian.com>
 
        * ApplicationSettingsBaseTest.cs : added test for bug #78654.
index b6f7713930d90beea18a26c3be1762808376ac68..0b946419c10470bcc40e84f431bac5ac7807dcfe 100644 (file)
@@ -161,6 +161,48 @@ namespace MonoTests.System.Configuration {
                        Assert.IsFalse (v.UsingDefaultValue, "A4");
                }
 
+               [Test]
+               public void String_Deserialize ()
+               {
+                       SettingsProperty p = new SettingsProperty ("property",
+                                                                  typeof (int),
+                                                                  null,
+                                                                  true,
+                                                                  10,
+                                                                  SettingsSerializeAs.String,
+                                                                  null,
+                                                                  true,
+                                                                  false);
+
+                       SettingsPropertyValue v = new SettingsPropertyValue (p);
+                       v.SerializedValue = "123";
+
+                       Assert.AreEqual (123, v.PropertyValue, "A1");
+                       Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
+                       Assert.AreEqual (false, v.UsingDefaultValue, "A3");
+               }
+
+               [Test]
+               public void Xml_Deserialize ()
+               {
+                       SettingsProperty p = new SettingsProperty ("property",
+                                                                  typeof (int),
+                                                                  null,
+                                                                  true,
+                                                                  10,
+                                                                  SettingsSerializeAs.Xml,
+                                                                  null,
+                                                                  true,
+                                                                  false);
+
+                       SettingsPropertyValue v = new SettingsPropertyValue (p);
+                       v.SerializedValue = "<?xml version=\"1.0\" encoding=\"utf-16\"?>\n<int>123</int>";
+
+                       Assert.AreEqual (123, v.PropertyValue, "A1");
+                       Assert.AreEqual (typeof(int), v.PropertyValue.GetType (), "A2");
+                       Assert.AreEqual (false, v.UsingDefaultValue, "A3");
+               }
+
                [Test]
                public void String_Xml_Serialize ()
                {