2 // System.Web.UI.WebControls.SettingsPropertyValue.cs
5 // Chris Toshok (toshok@ximian.com)
7 // (C) 2005 Novell, Inc (http://www.novell.com)
9 // Permission is hereby granted, free of charge, to any person obtaining
10 // a copy of this software and associated documentation files (the
11 // "Software"), to deal in the Software without restriction, including
12 // without limitation the rights to use, copy, modify, merge, publish,
13 // distribute, sublicense, and/or sell copies of the Software, and to
14 // permit persons to whom the Software is furnished to do so, subject to
15 // the following conditions:
17 // The above copyright notice and this permission notice shall be
18 // included in all copies or substantial portions of the Software.
20 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
21 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
22 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
23 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
24 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
25 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
26 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
32 using System.ComponentModel;
33 using System.Runtime.Serialization.Formatters.Binary;
36 using System.Xml.Serialization;
39 namespace System.Configuration
42 public class SettingsPropertyValue
44 public SettingsPropertyValue (SettingsProperty property)
46 this.property = property;
47 needPropertyValue = true;
50 public bool Deserialized {
74 public SettingsProperty Property {
80 public object PropertyValue {
82 if (needPropertyValue) {
83 propertyValue = GetDeserializedValue ();
84 if (propertyValue == null) {
85 propertyValue = property.DefaultValue;
88 needPropertyValue = false;
92 /* LAMESPEC: the msdn2 docs say that
93 * for object types this
94 * pessimistically sets Dirty == true.
95 * tests, however, point out that that
97 if (!property.PropertyType.IsValueType)
101 return propertyValue;
104 propertyValue = value;
106 needPropertyValue = false;
107 needSerializedValue = true;
112 [MonoTODO ("string type converter?")]
113 public object SerializedValue {
115 if (needSerializedValue) {
116 needSerializedValue = false;
118 switch (property.SerializeAs)
120 case SettingsSerializeAs.String:
121 /* the docs say use a string type converter.. this means what? */
122 serializedValue = propertyValue.ToString();
125 case SettingsSerializeAs.Xml:
126 XmlSerializer serializer = new XmlSerializer (propertyValue.GetType());
127 StringWriter w = new StringWriter();
129 serializer.Serialize (w, propertyValue);
130 serializedValue = w.ToString();
133 case SettingsSerializeAs.Binary:
134 BinaryFormatter bf = new BinaryFormatter ();
135 MemoryStream ms = new MemoryStream ();
136 bf.Serialize (ms, propertyValue);
137 serializedValue = ms.ToArray();
140 serializedValue = null;
146 return serializedValue;
149 serializedValue = value;
150 needPropertyValue = true;
154 public bool UsingDefaultValue {
160 private object GetDeserializedValue ()
162 if (serializedValue == null)
165 object deserializedObject = null;
168 switch (property.SerializeAs) {
169 case SettingsSerializeAs.String:
170 if (((string) serializedValue).Length > 0)
171 deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromString ((string) serializedValue);
174 case SettingsSerializeAs.Xml:
175 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
176 StringReader str = new StringReader ((string) serializedValue);
177 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
180 case SettingsSerializeAs.Binary:
181 BinaryFormatter bf = new BinaryFormatter ();
182 MemoryStream ms = new MemoryStream ((byte []) serializedValue);
183 deserializedObject = bf.Deserialize (ms);
187 catch (Exception e) {
188 if (property.ThrowOnErrorDeserializing)
192 return deserializedObject;
195 SettingsProperty property;
196 object propertyValue;
197 object serializedValue;
198 bool needSerializedValue;
199 bool needPropertyValue;
201 bool defaulted = false;