SettingsPropertyValue.cs 6.3 KB

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