SettingsPropertyValue.cs 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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.IO;
  31. using System.Runtime.Serialization.Formatters.Binary;
  32. #if (XML_DEP)
  33. using System.Xml.Serialization;
  34. #endif
  35. namespace System.Configuration
  36. {
  37. public class SettingsPropertyValue
  38. {
  39. public SettingsPropertyValue (SettingsProperty property)
  40. {
  41. this.property = property;
  42. needPropertyValue = true;
  43. }
  44. public bool Deserialized {
  45. get {
  46. return deserialized;
  47. }
  48. set {
  49. deserialized = true;
  50. }
  51. }
  52. public bool IsDirty {
  53. get {
  54. return dirty;
  55. }
  56. set {
  57. dirty = value;
  58. }
  59. }
  60. public string Name {
  61. get {
  62. return property.Name;
  63. }
  64. }
  65. public SettingsProperty Property {
  66. get {
  67. return property;
  68. }
  69. }
  70. public object PropertyValue {
  71. get {
  72. if (needPropertyValue) {
  73. needPropertyValue = false;
  74. propertyValue = property.DefaultValue;
  75. defaulted = true;
  76. }
  77. #if notyet
  78. /* LAMESPEC: the msdn2 docs say that
  79. * for object types this
  80. * pessimistically sets Dirty == true.
  81. * tests, however, point out that that
  82. * is not the case. */
  83. if (!property.PropertyType.IsValueType)
  84. dirty = true;
  85. #endif
  86. return propertyValue;
  87. }
  88. set {
  89. propertyValue = value;
  90. dirty = true;
  91. needPropertyValue = false;
  92. needSerializedValue = true;
  93. defaulted = false;
  94. }
  95. }
  96. [MonoTODO ("string type converter?")]
  97. public object SerializedValue {
  98. get {
  99. if (needSerializedValue) {
  100. needSerializedValue = false;
  101. switch (property.SerializeAs)
  102. {
  103. case SettingsSerializeAs.String:
  104. /* the docs say use a string type converter.. this means what? */
  105. serializedValue = propertyValue.ToString();
  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. }
  131. }
  132. public bool UsingDefaultValue {
  133. get {
  134. return defaulted;
  135. }
  136. }
  137. SettingsProperty property;
  138. object propertyValue;
  139. object serializedValue;
  140. bool needSerializedValue;
  141. bool needPropertyValue;
  142. bool dirty;
  143. bool defaulted;
  144. bool deserialized;
  145. }
  146. }
  147. #endif