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.
30 using System.Globalization;
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 (serializedValue);
84 if (propertyValue == null) {
85 propertyValue = GetDeserializedDefaultValue ();
88 needPropertyValue = false;
91 if (propertyValue != null &&
92 !(propertyValue is string) &&
93 !(propertyValue is DateTime) &&
94 !property.PropertyType.IsPrimitive)
100 propertyValue = value;
102 needPropertyValue = false;
103 needSerializedValue = true;
108 public object SerializedValue {
110 if (needSerializedValue) {
111 needSerializedValue = false;
113 switch (property.SerializeAs)
115 case SettingsSerializeAs.String:
116 serializedValue = TypeDescriptor.GetConverter (property.PropertyType).ConvertToInvariantString (propertyValue);
119 case SettingsSerializeAs.Xml:
120 if (propertyValue != null) {
121 XmlSerializer serializer = new XmlSerializer (propertyValue.GetType ());
122 StringWriter w = new StringWriter(CultureInfo.InvariantCulture);
124 serializer.Serialize (w, propertyValue);
125 serializedValue = w.ToString();
128 serializedValue = null;
131 case SettingsSerializeAs.Binary:
132 if (propertyValue != null) {
133 BinaryFormatter bf = new BinaryFormatter ();
134 MemoryStream ms = new MemoryStream ();
135 bf.Serialize (ms, propertyValue);
136 serializedValue = ms.ToArray();
139 serializedValue = null;
142 serializedValue = null;
148 return serializedValue;
151 serializedValue = value;
152 needPropertyValue = true;
156 public bool UsingDefaultValue {
162 internal object Reset ()
164 propertyValue = GetDeserializedDefaultValue ();
167 needPropertyValue = true;
168 return propertyValue;
171 private object GetDeserializedDefaultValue ()
173 if (property.DefaultValue == null)
174 if (property.PropertyType.IsValueType)
175 return Activator.CreateInstance (property.PropertyType);
179 if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
180 if (property.PropertyType != typeof (string))
181 return Activator.CreateInstance (property.PropertyType);
185 if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
186 return GetDeserializedValue (property.DefaultValue);
188 if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
189 TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
190 return converter.ConvertFrom (null, CultureInfo.InvariantCulture, property.DefaultValue);
192 return property.DefaultValue;
195 private object GetDeserializedValue (object serializedValue)
197 if (serializedValue == null)
200 object deserializedObject = null;
203 switch (property.SerializeAs) {
204 case SettingsSerializeAs.String:
205 if (serializedValue is string)
206 deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromInvariantString ((string) serializedValue);
209 case SettingsSerializeAs.Xml:
210 XmlSerializer serializer = new XmlSerializer (property.PropertyType);
211 StringReader str = new StringReader ((string) serializedValue);
212 deserializedObject = serializer.Deserialize (XmlReader.Create (str));
215 case SettingsSerializeAs.Binary:
216 BinaryFormatter bf = new BinaryFormatter ();
218 if (serializedValue is string)
219 ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
221 ms = new MemoryStream ((byte []) serializedValue);
222 deserializedObject = bf.Deserialize (ms);
226 catch (Exception e) {
227 if (property.ThrowOnErrorDeserializing)
231 return deserializedObject;
234 readonly SettingsProperty property;
235 object propertyValue;
236 object serializedValue;
237 bool needSerializedValue;
238 bool needPropertyValue;
240 bool defaulted = false;