SettingsPropertyValue.cs 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. //
  2. // System.Web.UI.WebControls.SettingsPropertyValue.cs
  3. //
  4. // Authors:
  5. // Chris Toshok ([email protected])
  6. //
  7. // (C) 2005 Novell, Inc (http://www.novell.com)
  8. //
  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:
  16. //
  17. // The above copyright notice and this permission notice shall be
  18. // included in all copies or substantial portions of the Software.
  19. //
  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.
  27. //
  28. #if NET_2_0
  29. using System;
  30. using System.Globalization;
  31. using System.IO;
  32. using System.ComponentModel;
  33. using System.Runtime.Serialization.Formatters.Binary;
  34. #if (XML_DEP)
  35. using System.Xml;
  36. using System.Xml.Serialization;
  37. #endif
  38. namespace System.Configuration
  39. {
  40. public class SettingsPropertyValue
  41. {
  42. public SettingsPropertyValue (SettingsProperty property)
  43. {
  44. this.property = property;
  45. needPropertyValue = true;
  46. }
  47. public bool Deserialized {
  48. get {
  49. return deserialized;
  50. }
  51. set {
  52. deserialized = value;
  53. }
  54. }
  55. public bool IsDirty {
  56. get {
  57. return dirty;
  58. }
  59. set {
  60. dirty = value;
  61. }
  62. }
  63. public string Name {
  64. get {
  65. return property.Name;
  66. }
  67. }
  68. public SettingsProperty Property {
  69. get {
  70. return property;
  71. }
  72. }
  73. public object PropertyValue {
  74. get {
  75. if (needPropertyValue) {
  76. propertyValue = GetDeserializedValue (serializedValue);
  77. if (propertyValue == null) {
  78. propertyValue = GetDeserializedDefaultValue ();
  79. defaulted = true;
  80. }
  81. needPropertyValue = false;
  82. }
  83. if (propertyValue != null &&
  84. !(propertyValue is string) &&
  85. !(propertyValue is DateTime) &&
  86. !property.PropertyType.IsPrimitive)
  87. dirty = true;
  88. return propertyValue;
  89. }
  90. set {
  91. propertyValue = value;
  92. dirty = true;
  93. needPropertyValue = false;
  94. needSerializedValue = true;
  95. defaulted = false;
  96. }
  97. }
  98. public object SerializedValue {
  99. get {
  100. if (needSerializedValue) {
  101. needSerializedValue = false;
  102. switch (property.SerializeAs)
  103. {
  104. case SettingsSerializeAs.String:
  105. serializedValue = TypeDescriptor.GetConverter (property.PropertyType).ConvertToString (propertyValue);
  106. break;
  107. #if (XML_DEP)
  108. case SettingsSerializeAs.Xml:
  109. XmlSerializer serializer = new XmlSerializer (propertyValue.GetType());
  110. StringWriter w = new StringWriter();
  111. serializer.Serialize (w, propertyValue);
  112. serializedValue = w.ToString();
  113. break;
  114. #endif
  115. case SettingsSerializeAs.Binary:
  116. BinaryFormatter bf = new BinaryFormatter ();
  117. MemoryStream ms = new MemoryStream ();
  118. bf.Serialize (ms, propertyValue);
  119. serializedValue = ms.ToArray();
  120. break;
  121. default:
  122. serializedValue = null;
  123. break;
  124. }
  125. }
  126. return serializedValue;
  127. }
  128. set {
  129. serializedValue = value;
  130. needPropertyValue = true;
  131. }
  132. }
  133. public bool UsingDefaultValue {
  134. get {
  135. return defaulted;
  136. }
  137. }
  138. private object GetDeserializedDefaultValue ()
  139. {
  140. if (property.DefaultValue == null)
  141. if (property.PropertyType.IsValueType)
  142. return Activator.CreateInstance (property.PropertyType);
  143. else
  144. return null;
  145. if (property.DefaultValue is string && ((string) property.DefaultValue).Length == 0)
  146. if (property.PropertyType != typeof (string))
  147. return Activator.CreateInstance (property.PropertyType);
  148. else
  149. return string.Empty;
  150. if (property.DefaultValue is string && ((string) property.DefaultValue).Length > 0)
  151. return GetDeserializedValue (property.DefaultValue);
  152. if (!property.PropertyType.IsAssignableFrom (property.DefaultValue.GetType ())) {
  153. TypeConverter converter = TypeDescriptor.GetConverter (property.PropertyType);
  154. return converter.ConvertFrom (property.DefaultValue);
  155. }
  156. return property.DefaultValue;
  157. }
  158. private object GetDeserializedValue (object serializedValue)
  159. {
  160. if (serializedValue == null)
  161. return null;
  162. object deserializedObject = null;
  163. try {
  164. switch (property.SerializeAs) {
  165. case SettingsSerializeAs.String:
  166. if (serializedValue is string && ((string) serializedValue).Length > 0)
  167. deserializedObject = TypeDescriptor.GetConverter (property.PropertyType).ConvertFromString ((string) serializedValue);
  168. break;
  169. #if (XML_DEP)
  170. case SettingsSerializeAs.Xml:
  171. XmlSerializer serializer = new XmlSerializer (property.PropertyType);
  172. StringReader str = new StringReader ((string) serializedValue);
  173. deserializedObject = serializer.Deserialize (XmlReader.Create (str));
  174. break;
  175. #endif
  176. case SettingsSerializeAs.Binary:
  177. BinaryFormatter bf = new BinaryFormatter ();
  178. MemoryStream ms;
  179. if (serializedValue is string)
  180. ms = new MemoryStream (Convert.FromBase64String ((string) serializedValue));
  181. else
  182. ms = new MemoryStream ((byte []) serializedValue);
  183. deserializedObject = bf.Deserialize (ms);
  184. break;
  185. }
  186. }
  187. catch (Exception e) {
  188. if (property.ThrowOnErrorDeserializing)
  189. throw e;
  190. }
  191. return deserializedObject;
  192. }
  193. readonly SettingsProperty property;
  194. object propertyValue;
  195. object serializedValue;
  196. bool needSerializedValue;
  197. bool needPropertyValue;
  198. bool dirty;
  199. bool defaulted = false;
  200. bool deserialized;
  201. }
  202. }
  203. #endif