PropertyCodeDomSerializer.cs 8.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221
  1. //
  2. // System.ComponentModel.Design.Serialization.PropertyCodeDomSerializer
  3. //
  4. // Authors:
  5. // Ivan N. Zlatev (contact i-nZ.net)
  6. //
  7. // (C) 2007 Ivan N. Zlatev
  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.ComponentModel;
  31. using System.ComponentModel.Design;
  32. using System.CodeDom;
  33. namespace System.ComponentModel.Design.Serialization
  34. {
  35. internal class PropertyCodeDomSerializer : MemberCodeDomSerializer
  36. {
  37. public PropertyCodeDomSerializer ()
  38. {
  39. }
  40. public override void Serialize (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor, CodeStatementCollection statements)
  41. {
  42. if (manager == null)
  43. throw new ArgumentNullException ("manager");
  44. if (value == null)
  45. throw new ArgumentNullException ("value");
  46. if (descriptor == null)
  47. throw new ArgumentNullException ("descriptor");
  48. if (statements == null)
  49. throw new ArgumentNullException ("statements");
  50. PropertyDescriptor property = (PropertyDescriptor) descriptor;
  51. if (property.Attributes.Contains (DesignerSerializationVisibilityAttribute.Content))
  52. SerializeContentProperty (manager, value, property, statements);
  53. else if (!property.Attributes.Contains (DesignerSerializationVisibilityAttribute.Hidden))
  54. SerializeNormalProperty (manager, value, property, statements);
  55. }
  56. private void SerializeNormalProperty (IDesignerSerializationManager manager,
  57. object component, PropertyDescriptor descriptor, CodeStatementCollection statements)
  58. {
  59. CodeAssignStatement assignment = new CodeAssignStatement ();
  60. CodeExpression leftSide = null;
  61. CodePropertyReferenceExpression propRef = null;
  62. ExpressionContext expression = manager.Context[typeof (ExpressionContext)] as ExpressionContext;
  63. RootContext root = manager.Context[typeof (RootContext)] as RootContext;
  64. if (expression != null && expression.PresetValue == component && expression.Expression != null) {
  65. leftSide = new CodePropertyReferenceExpression (expression.Expression, descriptor.Name);
  66. } else if (root != null && root.Value == component) {
  67. leftSide = new CodePropertyReferenceExpression (root.Expression, descriptor.Name);
  68. } else {
  69. propRef = new CodePropertyReferenceExpression ();
  70. propRef.PropertyName = descriptor.Name;
  71. propRef.TargetObject = TryGetCachedExpression (manager, component, propRef);
  72. leftSide = propRef;
  73. }
  74. CodeExpression rightSide = null;
  75. MemberRelationship relationship = GetRelationship (manager, component, descriptor);
  76. if (!relationship.IsEmpty) {
  77. propRef = new CodePropertyReferenceExpression ();
  78. propRef.PropertyName = relationship.Member.Name;
  79. propRef.TargetObject = TryGetCachedExpression (manager, relationship.Owner, propRef);
  80. rightSide = propRef;
  81. } else {
  82. object rightSideValue = descriptor.GetValue (component);
  83. rightSide = TryGetCachedExpression (manager, rightSideValue, null, component);
  84. }
  85. if (rightSide == null || leftSide == null) {
  86. base.ReportError (manager, "Cannot serialize " + ((IComponent)component).Site.Name + "." + descriptor.Name,
  87. "Property Name: " + descriptor.Name + System.Environment.NewLine +
  88. "Property Type: " + descriptor.PropertyType.Name + System.Environment.NewLine);
  89. } else {
  90. assignment.Left = leftSide;
  91. assignment.Right = rightSide;
  92. statements.Add (assignment);
  93. }
  94. }
  95. private CodeExpression TryGetCachedExpression (IDesignerSerializationManager manager, object value)
  96. {
  97. return TryGetCachedExpression (manager, value, null);
  98. }
  99. private CodeExpression TryGetCachedExpression (IDesignerSerializationManager manager, object value,
  100. CodeExpression parentExpression)
  101. {
  102. return TryGetCachedExpression (manager, value, parentExpression, null);
  103. }
  104. private CodeExpression TryGetCachedExpression (IDesignerSerializationManager manager, object value,
  105. CodeExpression parentExpression, object presetValue)
  106. {
  107. CodeExpression expression = null;
  108. if (value != null) // in order to support null value serialization
  109. expression = base.GetExpression (manager, value);
  110. if (expression == null) {
  111. if (parentExpression == null)
  112. manager.Context.Push (new ExpressionContext (null, null, value, presetValue));
  113. else
  114. manager.Context.Push (new ExpressionContext (parentExpression, parentExpression.GetType (), value, presetValue));
  115. expression = base.SerializeToExpression (manager, value);
  116. manager.Context.Pop ();
  117. }
  118. return expression;
  119. }
  120. private void SerializeContentProperty (IDesignerSerializationManager manager, object component,
  121. PropertyDescriptor descriptor, CodeStatementCollection statements)
  122. {
  123. CodePropertyReferenceExpression propRef = new CodePropertyReferenceExpression ();
  124. propRef.PropertyName = descriptor.Name;
  125. object value = descriptor.GetValue (component);
  126. ExpressionContext expressionCtx = manager.Context[typeof (ExpressionContext)] as ExpressionContext;
  127. if (expressionCtx != null && expressionCtx.PresetValue == component) {
  128. propRef.TargetObject = expressionCtx.Expression;
  129. } else {
  130. manager.Context.Push (new CodeStatementCollection ());
  131. propRef.TargetObject = TryGetCachedExpression (manager, component, propRef, value);
  132. manager.Context.Pop ();
  133. }
  134. CodeDomSerializer serializer = manager.GetSerializer (value.GetType (), typeof (CodeDomSerializer)) as CodeDomSerializer;
  135. if (propRef.TargetObject != null && serializer != null) {
  136. // request full serialization (presetvalue == instance)
  137. //
  138. manager.Context.Push (new ExpressionContext (propRef, propRef.GetType (), component, value));
  139. object serialized = serializer.Serialize (manager, value);
  140. manager.Context.Pop ();
  141. CodeStatementCollection serializedStatements = serialized as CodeStatementCollection;
  142. if (serializedStatements != null)
  143. statements.AddRange (serializedStatements);
  144. CodeStatement serializedStatement = serialized as CodeStatement;
  145. if (serializedStatement != null)
  146. statements.Add (serializedStatement);
  147. CodeExpression serializedExpr = serialized as CodeExpression;
  148. if (serializedExpr != null)
  149. statements.Add (new CodeAssignStatement (propRef, serializedExpr));
  150. }
  151. }
  152. public override bool ShouldSerialize (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor)
  153. {
  154. if (manager == null)
  155. throw new ArgumentNullException ("manager");
  156. if (value == null)
  157. throw new ArgumentNullException ("value");
  158. if (descriptor == null)
  159. throw new ArgumentNullException ("descriptor");
  160. PropertyDescriptor property = (PropertyDescriptor) descriptor;
  161. if (property.Attributes.Contains (DesignerSerializationVisibilityAttribute.Hidden))
  162. return false;
  163. else if (property.Attributes.Contains (DesignOnlyAttribute.Yes))
  164. return false;
  165. bool result = property.ShouldSerializeValue (value);
  166. if (!result) {
  167. if (!GetRelationship (manager, value, descriptor).IsEmpty)
  168. result = true;
  169. }
  170. if (!result) {
  171. SerializeAbsoluteContext absolute = manager.Context[typeof (SerializeAbsoluteContext)] as SerializeAbsoluteContext;
  172. if (absolute != null && absolute.ShouldSerialize (descriptor))
  173. result = true;
  174. }
  175. return result;
  176. }
  177. private MemberRelationship GetRelationship (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor)
  178. {
  179. MemberRelationshipService service = manager.GetService (typeof (MemberRelationshipService)) as MemberRelationshipService;
  180. if (service != null)
  181. return service[value, descriptor];
  182. else
  183. return MemberRelationship.Empty;
  184. }
  185. }
  186. }
  187. #endif