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.
31 using System.Globalization;
33 using System.ComponentModel;
34 using System.Runtime.Serialization.Formatters.Binary;
37 using System.Xml.Serialization;
40 namespace System.Configuration
43 public class SettingsPropertyValue
45 public SettingsPropertyValue (SettingsProperty property)
47 this.property = property;
48 needPropertyValue = true;
51 public bool Deserialized {
75 public SettingsProperty Property {
81 public object PropertyValue {
83 if (needPropertyValue) {
84 propertyValue = GetDeserializedValue (serializedValue);
85 if (propertyValue == null) {
86 propertyValue = GetDeserializedDefaultValue ();
89 needPropertyValue = false;
92 if (propertyValue != null &&
93 !(propertyValue is string) &&
94 !(propertyValue is DateTime) &&
95 !property.PropertyType.IsPrimitive)
101 propertyValue = value;
103 needPropertyValue = false;
104 needSerializedValue = true;
109 public object SerializedValue {
111 if (needSerializedValue) {
112 needSerializedValue = false;
114 switch (property.SerializeAs)
116 case SettingsSerializeAs.String:
117 serializedValue = TypeDescriptor.GetConverter (property.PropertyType).ConvertToInvariantString (propertyValue);
120 case SettingsSerializeAs.Xml:
121 if (propertyValue != null) {
122 XmlSerializer serializer = new XmlSerializer (propertyValue.GetType ());
123 StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
125 serializer.Serialize (w, propertyValue);
126 serializedValue = w.ToString();
129 serializedValue = null;
132 case SettingsSerializeAs.Binary:
133 if (propertyValue != null) {
134 BinaryFormatter bf = new BinaryFormatter ();
135 MemoryStream ms = new MemoryStream ();
136 bf.Serialize (ms, propertyValue);
137 serializedValue = ms.ToArray();
140 serializedValue = null;
143 serializedValue = null;
149 return serializedValue;
152 serializedValue = value;
153 needPropertyValue = true;
157 public bool UsingDefaultValue {
163 internal object Reset ()
165 propertyValue = GetDeserializedDefaultValue ();
168 needPropertyValue = true;
169 return propertyValue;
172 private object GetDeserializedDefaultValue ()
174 if (property.DefaultValue == null)
175 if (property.PropertyType.IsValueType)
176 return Activator.CreateInstance (property.PropertyType);
180 if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
181 if (property.PropertyType != typeof (string))
182 return Activator.CreateInstance (property.PropertyType);
186 if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
187 return GetDeserializedValue (property.DefaultValue);
189 if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
190 TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
191 return converter.ConvertFrom (null, CultureInfo.InvariantCulture, property.DefaultValue);
193 return property.DefaultValue;
196 private object GetDeserializedValue (object serializedValue)
198 if (serializedValue == null)
201 object deserializedObject = null;
204 switch (property.SerializeAs) {
205 case SettingsSerializeAs.String:
206 if (serializedValue is string)
207 deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromInvariantString ((string) serializedValue);
210 case SettingsSerializeAs.Xml:
211 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
212 StringReader str = new StringReader ((string) serializedValue);
213 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
216 case SettingsSerializeAs.Binary:
217 BinaryFormatter bf = new BinaryFormatter ();
219 if (serializedValue is string)
220 ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
222 ms = new MemoryStream ((byte []) serializedValue);
223 deserializedObject = bf.Deserialize (ms);
227 catch (Exception e) {
228 if (property.ThrowOnErrorDeserializing)
232 return deserializedObject;
235 readonly SettingsProperty property;
236 object propertyValue;
237 object serializedValue;
238 bool needSerializedValue;
239 bool needPropertyValue;
241 bool defaulted = false;