PropertyCodeDomSerializer.cs 7.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186
  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 instance, 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 == instance && expression.Expression != null) {
  65. leftSide = new CodePropertyReferenceExpression (expression.Expression, descriptor.Name);
  66. } else if (root != null && root.Value == instance) {
  67. leftSide = new CodePropertyReferenceExpression (root.Expression, descriptor.Name);
  68. } else {
  69. propRef = new CodePropertyReferenceExpression ();
  70. propRef.PropertyName = descriptor.Name;
  71. propRef.TargetObject = base.SerializeToExpression (manager, instance);
  72. leftSide = propRef;
  73. }
  74. CodeExpression rightSide = null;
  75. MemberRelationship relationship = GetRelationship (manager, instance, descriptor);
  76. if (!relationship.IsEmpty) {
  77. propRef = new CodePropertyReferenceExpression ();
  78. propRef.PropertyName = relationship.Member.Name;
  79. propRef.TargetObject = base.SerializeToExpression (manager, relationship.Owner);
  80. rightSide = propRef;
  81. } else {
  82. rightSide = base.SerializeToExpression (manager, descriptor.GetValue (instance));
  83. }
  84. if (rightSide == null || leftSide == null) {
  85. base.ReportError (manager, "Cannot serialize " + ((IComponent)instance).Site.Name + "." + descriptor.Name,
  86. "Property Name: " + descriptor.Name + System.Environment.NewLine +
  87. "Property Type: " + descriptor.PropertyType.Name + System.Environment.NewLine);
  88. } else {
  89. assignment.Left = leftSide;
  90. assignment.Right = rightSide;
  91. statements.Add (assignment);
  92. }
  93. }
  94. private void SerializeContentProperty (IDesignerSerializationManager manager, object instance,
  95. PropertyDescriptor descriptor, CodeStatementCollection statements)
  96. {
  97. CodePropertyReferenceExpression propRef = new CodePropertyReferenceExpression ();
  98. propRef.PropertyName = descriptor.Name;
  99. object propertyValue = descriptor.GetValue (instance);
  100. ExpressionContext expressionCtx = manager.Context[typeof (ExpressionContext)] as ExpressionContext;
  101. if (expressionCtx != null && expressionCtx.PresetValue == instance)
  102. propRef.TargetObject = expressionCtx.Expression;
  103. else
  104. propRef.TargetObject = base.SerializeToExpression (manager, instance);
  105. CodeDomSerializer serializer = manager.GetSerializer (propertyValue.GetType (), typeof (CodeDomSerializer)) as CodeDomSerializer;
  106. if (propRef.TargetObject != null && serializer != null) {
  107. manager.Context.Push (new ExpressionContext (propRef, propRef.GetType (), null, propertyValue));
  108. object serialized = serializer.Serialize (manager, propertyValue);
  109. manager.Context.Pop ();
  110. CodeStatementCollection serializedStatements = serialized as CodeStatementCollection;
  111. if (serializedStatements != null)
  112. statements.AddRange (serializedStatements);
  113. CodeStatement serializedStatement = serialized as CodeStatement;
  114. if (serializedStatement != null)
  115. statements.Add (serializedStatement);
  116. CodeExpression serializedExpr = serialized as CodeExpression;
  117. if (serializedExpr != null)
  118. statements.Add (new CodeAssignStatement (propRef, serializedExpr));
  119. }
  120. }
  121. public override bool ShouldSerialize (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor)
  122. {
  123. if (manager == null)
  124. throw new ArgumentNullException ("manager");
  125. if (value == null)
  126. throw new ArgumentNullException ("value");
  127. if (descriptor == null)
  128. throw new ArgumentNullException ("descriptor");
  129. PropertyDescriptor property = (PropertyDescriptor) descriptor;
  130. if (property.Attributes.Contains (DesignerSerializationVisibilityAttribute.Hidden))
  131. return false;
  132. else if (property.Attributes.Contains (DesignOnlyAttribute.Yes))
  133. return false;
  134. bool result = property.ShouldSerializeValue (value);
  135. if (!result) {
  136. if (!GetRelationship (manager, value, descriptor).IsEmpty)
  137. result = true;
  138. }
  139. if (!result) {
  140. SerializeAbsoluteContext absolute = manager.Context[typeof (SerializeAbsoluteContext)] as SerializeAbsoluteContext;
  141. if (absolute != null && absolute.ShouldSerialize (descriptor))
  142. result = true;
  143. }
  144. return result;
  145. }
  146. private MemberRelationship GetRelationship (IDesignerSerializationManager manager, object value, MemberDescriptor descriptor)
  147. {
  148. MemberRelationshipService service = manager.GetService (typeof (MemberRelationshipService)) as MemberRelationshipService;
  149. if (service != null)
  150. return service[value, descriptor];
  151. else
  152. return MemberRelationship.Empty;
  153. }
  154. }
  155. }
  156. #endif