RootCodeDomSerializer.cs 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253
  1. //
  2. // System.ComponentModel.Design.Serialization.RootCodeDomSerializer
  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.Collections;
  31. using System.Collections.Generic;
  32. using System.ComponentModel;
  33. using System.ComponentModel.Design;
  34. using System.CodeDom;
  35. namespace System.ComponentModel.Design.Serialization
  36. {
  37. internal class RootCodeDomSerializer : CodeDomSerializer
  38. {
  39. internal class CodeMap
  40. {
  41. private string _className;
  42. private Type _classType;
  43. private List<CodeMemberField> _fields;
  44. private CodeStatementCollection _initializers;
  45. private CodeStatementCollection _begin;
  46. private CodeStatementCollection _default;
  47. private CodeStatementCollection _end;
  48. public CodeMap (Type classType, string className)
  49. {
  50. if (classType == null)
  51. throw new ArgumentNullException ("classType");
  52. if (className == null)
  53. throw new ArgumentNullException ("className");
  54. _classType = classType;
  55. _className = className;
  56. _fields = new List<CodeMemberField> ();
  57. _initializers = new CodeStatementCollection ();
  58. _begin = new CodeStatementCollection ();
  59. _default = new CodeStatementCollection ();
  60. _end = new CodeStatementCollection ();
  61. }
  62. public void AddField (CodeMemberField field)
  63. {
  64. _fields.Add (field);
  65. }
  66. public void Add (CodeStatementCollection statements)
  67. {
  68. foreach (CodeStatement statement in statements)
  69. this.Add (statement);
  70. }
  71. public void Add (CodeStatement statement)
  72. {
  73. if (statement.UserData["statement-order"] == null)
  74. _default.Add (statement);
  75. else if ((string)statement.UserData["statement-order"] == "initializer")
  76. _initializers.Add (statement);
  77. else if ((string)statement.UserData["statement-order"] == "begin")
  78. _begin.Add (statement);
  79. else if ((string)statement.UserData["statement-order"] == "end")
  80. _end.Add (statement);
  81. }
  82. /*
  83. class Type : BaseType
  84. {
  85. #region Windows Form Designer generated code
  86. private void InitializeComponent ()
  87. {
  88. // statement-order:
  89. initializer
  90. pre-begin - e.g: // ComponentName
  91. begin - e.g: SuspendLayout
  92. default
  93. end - e.g: ResumeLayout
  94. post-end
  95. }
  96. private field1;
  97. private field2;
  98. #endregion
  99. }
  100. */
  101. public CodeTypeDeclaration GenerateClass ()
  102. {
  103. CodeTypeDeclaration clas = new CodeTypeDeclaration (_className);
  104. clas.BaseTypes.Add (_classType);
  105. clas.StartDirectives.Add (new CodeRegionDirective (CodeRegionMode.Start, "Windows Form Designer generated code"));
  106. CodeMemberMethod initialize = new CodeMemberMethod ();
  107. initialize.Name = "InitializeComponent";
  108. initialize.ReturnType = new CodeTypeReference (typeof (void));
  109. initialize.Attributes = MemberAttributes.Private;
  110. initialize.Statements.AddRange (_initializers);
  111. initialize.Statements.AddRange (_begin);
  112. initialize.Statements.AddRange (_default);
  113. initialize.Statements.AddRange (_end);
  114. clas.Members.Add (initialize);
  115. foreach (CodeMemberField field in _fields)
  116. clas.Members.Add (field);
  117. clas.EndDirectives.Add (new CodeRegionDirective (CodeRegionMode.End, null));
  118. return clas;
  119. }
  120. public void Clear ()
  121. {
  122. _initializers.Clear ();
  123. _begin.Clear ();
  124. _default.Clear ();
  125. _end.Clear ();
  126. }
  127. }
  128. private CodeMap _codeMap;
  129. public RootCodeDomSerializer ()
  130. {
  131. }
  132. public override object Serialize (IDesignerSerializationManager manager, object value)
  133. {
  134. if (manager == null)
  135. throw new ArgumentNullException ("manager");
  136. if (value == null)
  137. throw new ArgumentNullException ("value");
  138. if (_codeMap == null)
  139. _codeMap = new CodeMap (value.GetType (), manager.GetName (value));
  140. _codeMap.Clear ();
  141. RootContext rootContext = new RootContext (new CodeThisReferenceExpression (), value);
  142. manager.Context.Push (rootContext);
  143. this.SerializeComponents (manager, ((IComponent) value).Site.Container.Components, (IComponent) value);
  144. // Serialize root component
  145. //
  146. CodeStatementCollection statements = new CodeStatementCollection ();
  147. statements.Add (new CodeCommentStatement (String.Empty));
  148. statements.Add (new CodeCommentStatement (manager.GetName (value)));
  149. statements.Add (new CodeCommentStatement (String.Empty));
  150. // Note that during the serialization process below ComponentCodeDomSerializer
  151. // will be invoked to serialize the rootcomponent during expression serialization.
  152. // It will check for RootContext and return that.
  153. base.SerializeProperties (manager, statements, value, new Attribute[0]);
  154. base.SerializeEvents (manager, statements, value, new Attribute[0]);
  155. _codeMap.Add (statements);
  156. manager.Context.Pop ();
  157. return _codeMap.GenerateClass ();
  158. }
  159. private void SerializeComponents (IDesignerSerializationManager manager, ICollection components, IComponent rootComponent)
  160. {
  161. foreach (IComponent component in components) {
  162. if (!Object.ReferenceEquals (component, rootComponent))
  163. SerializeComponent (manager, component);
  164. }
  165. }
  166. private void SerializeComponent (IDesignerSerializationManager manager, IComponent component)
  167. {
  168. CodeDomSerializer serializer = base.GetSerializer (manager, component) as CodeDomSerializer; // ComponentCodeDomSerializer
  169. if (serializer != null) {
  170. this._codeMap.AddField (new CodeMemberField (component.GetType (), manager.GetName (component)));
  171. // statements can be a CodeExpression if the full serialization has been completed prior
  172. // to this serialization call (e.g when it is requested during the serialization of another
  173. // component.
  174. //
  175. CodeStatementCollection statements = serializer.Serialize (manager, component) as CodeStatementCollection;
  176. if (statements != null)
  177. _codeMap.Add (statements);
  178. CodeStatement statement = serializer.Serialize (manager, component) as CodeStatement;
  179. if (statement != null)
  180. _codeMap.Add (statement);
  181. }
  182. }
  183. public override object Deserialize (IDesignerSerializationManager manager, object codeObject)
  184. {
  185. CodeTypeDeclaration declaration = (CodeTypeDeclaration) codeObject;
  186. Type rootType = manager.GetType (declaration.BaseTypes[0].BaseType);
  187. object root = manager.CreateInstance (rootType, null, declaration.Name, true);
  188. RootContext rootContext = new RootContext (new CodeThisReferenceExpression (), root);
  189. manager.Context.Push (rootContext);
  190. CodeMemberMethod initComponentMethod = GetInitializeMethod (declaration);
  191. if (initComponentMethod == null)
  192. throw new InvalidOperationException ("InitializeComponent method is missing in: " + declaration.Name);
  193. foreach (CodeStatement statement in initComponentMethod.Statements)
  194. base.DeserializeStatement (manager, statement);
  195. manager.Context.Pop ();
  196. return root;
  197. }
  198. private CodeMemberMethod GetInitializeMethod (CodeTypeDeclaration declaration)
  199. {
  200. CodeMemberMethod method = null;
  201. foreach (CodeTypeMember member in declaration.Members) {
  202. method = member as CodeMemberMethod;
  203. if (method != null && method.Name == "InitializeComponent")
  204. break;
  205. }
  206. return method;
  207. }
  208. }
  209. }
  210. #endif