2 // System.ComponentModel.Design.Serialization.ComponentCodeDomSerializer
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
.Collections
;
34 using System
.ComponentModel
;
35 using System
.ComponentModel
.Design
;
39 namespace System
.ComponentModel
.Design
.Serialization
41 // A serializer for the IComponent Type, supplied by the CodeDomSerializationProvider,
42 // added as a provider by the RootComponentCodeDomSerializer
44 internal class ComponentCodeDomSerializer
: CodeDomSerializer
47 public ComponentCodeDomSerializer ()
51 public override object Serialize (IDesignerSerializationManager manager
, object value)
54 throw new ArgumentNullException ("value");
56 throw new ArgumentNullException ("manager");
58 RootContext rootContext
= manager
.Context
[typeof (RootContext
)] as RootContext
;
59 if (rootContext
!= null && rootContext
.Value
== value)
60 return rootContext
.Expression
;
62 CodeStatementCollection statements
= new CodeStatementCollection ();
64 if (((IComponent
)value).Site
== null) {
65 ReportError (manager
, "Component of type '" + value.GetType().Name
+ "' not sited");
69 // the trick with the nested components is that GetName will return the full name
70 // e.g splitter1.Panel1 and thus the code below will create a reference to that.
72 string name
= manager
.GetName (value);
74 CodeExpression componentRef
= null;
75 if (rootContext
!= null)
76 componentRef
= new CodeFieldReferenceExpression (rootContext
.Expression
, name
);
78 componentRef
= new CodeFieldReferenceExpression (new CodeThisReferenceExpression () , name
);
80 base.SetExpression (manager
, value, componentRef
);
82 ExpressionContext context
= manager
.Context
[typeof (ExpressionContext
)] as ExpressionContext
;
83 // Perform some heuristics here.
85 // If there is an ExpressionContext of PropertyReference where PresetValue == this
86 // partial serialization doesn't make sense, so perform full. E.g in the case of:
88 // PropertyCodeDomSerializer.SerializeContentProperty and splitContainer1.*Panel1*
90 if (context
== null || context
.PresetValue
!= value ||
91 (context
.PresetValue
== value && (context
.Expression
is CodeFieldReferenceExpression
||
92 context
.Expression
is CodePropertyReferenceExpression
))) {
93 bool isComplete
= true;
94 statements
.Add (new CodeCommentStatement (String
.Empty
));
95 statements
.Add (new CodeCommentStatement (name
));
96 statements
.Add (new CodeCommentStatement (String
.Empty
));
98 // Do not serialize a creation expression for Nested components
100 if (! (((IComponent
)value).Site
is INestedSite
)) {
101 CodeStatement assignment
= new CodeAssignStatement (componentRef
,
102 base.SerializeCreationExpression (manager
, value,
104 assignment
.UserData
["statement-order"] = "initializer";
105 statements
.Add (assignment
);
108 base.SerializeProperties (manager
, statements
, value, new Attribute
[0]);
109 base.SerializeEvents (manager
, statements
, value);