2 // System.ComponentModel.Design.Serialization.PropertyCodeDomSerializer
5 // Ivan N. Zlatev (contact i-nZ.net)
7 // (C) 2007 Ivan N. Zlatev
10 // Permission is hereby granted, free of charge, to any person obtaining
11 // a copy of this software and associated documentation files (the
12 // "Software"), to deal in the Software without restriction, including
13 // without limitation the rights to use, copy, modify, merge, publish,
14 // distribute, sublicense, and/or sell copies of the Software, and to
15 // permit persons to whom the Software is furnished to do so, subject to
16 // the following conditions:
18 // The above copyright notice and this permission notice shall be
19 // included in all copies or substantial portions of the Software.
21 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
22 // EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
23 // MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
24 // NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
25 // LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
26 // OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
27 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
33 using System
.ComponentModel
;
34 using System
.ComponentModel
.Design
;
38 namespace System
.ComponentModel
.Design
.Serialization
40 internal class PropertyCodeDomSerializer
: MemberCodeDomSerializer
43 public PropertyCodeDomSerializer ()
47 public override void Serialize (IDesignerSerializationManager manager
, object value, MemberDescriptor descriptor
, CodeStatementCollection statements
)
50 throw new ArgumentNullException ("manager");
52 throw new ArgumentNullException ("value");
53 if (descriptor
== null)
54 throw new ArgumentNullException ("descriptor");
55 if (statements
== null)
56 throw new ArgumentNullException ("statements");
58 PropertyDescriptor property
= (PropertyDescriptor
) descriptor
;
60 if (property
.Attributes
.Contains (DesignerSerializationVisibilityAttribute
.Content
))
61 SerializeContentProperty (manager
, value, property
, statements
);
63 SerializeNormalProperty (manager
, value, property
, statements
);
67 private void SerializeNormalProperty (IDesignerSerializationManager manager
,
68 object instance
, PropertyDescriptor descriptor
, CodeStatementCollection statements
)
70 CodeAssignStatement assignment
= new CodeAssignStatement ();
72 CodeExpression leftSide
= null;
73 CodePropertyReferenceExpression propRef
= null;
74 ExpressionContext expression
= manager
.Context
[typeof (ExpressionContext
)] as ExpressionContext
;
75 RootContext root
= manager
.Context
[typeof (RootContext
)] as RootContext
;
77 if (expression
!= null && expression
.PresetValue
== instance
&& expression
.Expression
!= null) {
78 leftSide
= new CodePropertyReferenceExpression (expression
.Expression
, descriptor
.Name
);
79 } else if (root
!= null && root
.Value
== instance
) {
80 leftSide
= new CodePropertyReferenceExpression (root
.Expression
, descriptor
.Name
);
82 propRef
= new CodePropertyReferenceExpression ();
83 propRef
.PropertyName
= descriptor
.Name
;
84 propRef
.TargetObject
= base.SerializeToExpression (manager
, instance
);
88 CodeExpression rightSide
= null;
90 MemberRelationship relationship
= GetRelationship (manager
, instance
, descriptor
);
91 if (!relationship
.IsEmpty
) {
92 propRef
= new CodePropertyReferenceExpression ();
93 propRef
.PropertyName
= relationship
.Member
.Name
;
94 propRef
.TargetObject
= base.SerializeToExpression (manager
, relationship
.Owner
);
97 rightSide
= base.SerializeToExpression (manager
, descriptor
.GetValue (instance
));
100 if (rightSide
== null || leftSide
== null) {
101 base.ReportError (manager
, "Cannot serialize " + ((IComponent
)instance
).Site
.Name
+ "." + descriptor
.Name
,
102 "Property Name: " + descriptor
.Name
+ System
.Environment
.NewLine
+
103 "Property Type: " + descriptor
.PropertyType
.Name
+ System
.Environment
.NewLine
);
105 assignment
.Left
= leftSide
;
106 assignment
.Right
= rightSide
;
107 statements
.Add (assignment
);
111 private void SerializeContentProperty (IDesignerSerializationManager manager
, object instance
,
112 PropertyDescriptor descriptor
, CodeStatementCollection statements
)
114 CodePropertyReferenceExpression propRef
= new CodePropertyReferenceExpression ();
115 propRef
.PropertyName
= descriptor
.Name
;
116 object propertyValue
= descriptor
.GetValue (instance
);
118 ExpressionContext expressionCtx
= manager
.Context
[typeof (ExpressionContext
)] as ExpressionContext
;
119 if (expressionCtx
!= null && expressionCtx
.PresetValue
== instance
)
120 propRef
.TargetObject
= expressionCtx
.Expression
;
122 propRef
.TargetObject
= base.SerializeToExpression (manager
, instance
);
124 CodeDomSerializer serializer
= manager
.GetSerializer (propertyValue
.GetType (), typeof (CodeDomSerializer
)) as CodeDomSerializer
;
125 if (propRef
.TargetObject
!= null && serializer
!= null) {
126 manager
.Context
.Push (new ExpressionContext (propRef
, propRef
.GetType (), null, propertyValue
));
127 object serialized
= serializer
.Serialize (manager
, propertyValue
);
128 manager
.Context
.Pop ();
130 CodeStatementCollection serializedStatements
= serialized
as CodeStatementCollection
;
131 if (serializedStatements
!= null)
132 statements
.AddRange (serializedStatements
);
134 CodeStatement serializedStatement
= serialized
as CodeStatement
;
135 if (serializedStatement
!= null)
136 statements
.Add (serializedStatement
);
138 CodeExpression serializedExpr
= serialized
as CodeExpression
;
139 if (serializedExpr
!= null)
140 statements
.Add (new CodeAssignStatement (propRef
, serializedExpr
));
144 public override bool ShouldSerialize (IDesignerSerializationManager manager
, object value, MemberDescriptor descriptor
)
147 throw new ArgumentNullException ("manager");
149 throw new ArgumentNullException ("value");
150 if (descriptor
== null)
151 throw new ArgumentNullException ("descriptor");
153 PropertyDescriptor property
= (PropertyDescriptor
) descriptor
;
155 if (property
.Attributes
.Contains (DesignOnlyAttribute
.Yes
))
158 SerializeAbsoluteContext absolute
= manager
.Context
[typeof (SerializeAbsoluteContext
)] as SerializeAbsoluteContext
;
159 if (absolute
!= null && absolute
.ShouldSerialize (descriptor
))
162 bool result
= property
.ShouldSerializeValue (value);
165 if (!GetRelationship (manager
, value, descriptor
).IsEmpty
)
172 private MemberRelationship
GetRelationship (IDesignerSerializationManager manager
, object value, MemberDescriptor descriptor
)
174 MemberRelationshipService service
= manager
.GetService (typeof (MemberRelationshipService
)) as MemberRelationshipService
;
176 return service
[value, descriptor
];
178 return MemberRelationship
.Empty
;