1 // 2 // System.ComponentModel.Design.Serialization.CodeDomSerializer 3 // 4 // Authors: 5 // Ivan N. Zlatev (contact i-nZ.net) 6 // 7 // (C) 2007 Ivan N. Zlatev 8 9 // 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: 17 // 18 // The above copyright notice and this permission notice shall be 19 // included in all copies or substantial portions of the Software. 20 // 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. 28 // 29 30 31 using System; 32 using System.Collections; 33 using System.ComponentModel; 34 using System.ComponentModel.Design; 35 36 using System.CodeDom; 37 38 namespace System.ComponentModel.Design.Serialization 39 { 40 public class CodeDomSerializer : CodeDomSerializerBase 41 { 42 CodeDomSerializer()43 public CodeDomSerializer () 44 { 45 } 46 47 SerializeAbsolute(IDesignerSerializationManager manager, object value)48 public virtual object SerializeAbsolute (IDesignerSerializationManager manager, object value) 49 { 50 if (value == null) 51 throw new ArgumentNullException ("value"); 52 if (manager == null) 53 throw new ArgumentNullException ("manager"); 54 55 SerializeAbsoluteContext context = new SerializeAbsoluteContext (); 56 manager.Context.Push (context); 57 object result = this.Serialize (manager, value); 58 manager.Context.Pop (); 59 return result; 60 } 61 Serialize(IDesignerSerializationManager manager, object value)62 public virtual object Serialize (IDesignerSerializationManager manager, object value) 63 { 64 if (value == null) 65 throw new ArgumentNullException ("value"); 66 if (manager == null) 67 throw new ArgumentNullException ("manager"); 68 69 object serialized = null; 70 bool isComplete = false; 71 CodeExpression createExpr = base.SerializeCreationExpression (manager, value, out isComplete); 72 if (createExpr != null) { 73 if (isComplete) { 74 serialized = createExpr; 75 } else { 76 CodeStatementCollection statements = new CodeStatementCollection (); 77 base.SerializeProperties (manager, statements, value, new Attribute[0]); 78 base.SerializeEvents (manager, statements, value, new Attribute[0]); 79 serialized = statements; 80 } 81 base.SetExpression (manager, value, createExpr); 82 } 83 return serialized; 84 } 85 86 [Obsolete ("This method has been deprecated. Use SerializeToExpression or GetExpression instead.")] SerializeToReferenceExpression(IDesignerSerializationManager manager, object value)87 protected CodeExpression SerializeToReferenceExpression (IDesignerSerializationManager manager, object value) 88 { 89 return base.SerializeToExpression (manager, value); 90 } 91 92 // I am not sure what this does, but the only name I can think of this can get is a variable name from 93 // the expression GetTargetComponentName(CodeStatement statement, CodeExpression expression, Type targetType)94 public virtual string GetTargetComponentName (CodeStatement statement, CodeExpression expression, Type targetType) 95 { 96 if (expression is CodeFieldReferenceExpression) 97 return ((CodeFieldReferenceExpression) expression).FieldName; 98 else if (expression is CodeVariableReferenceExpression) 99 return ((CodeVariableReferenceExpression) expression).VariableName; 100 return null; 101 } 102 SerializeMember(IDesignerSerializationManager manager, object owningObject, MemberDescriptor member)103 public virtual CodeStatementCollection SerializeMember (IDesignerSerializationManager manager, 104 object owningObject, MemberDescriptor member) 105 { 106 if (member == null) 107 throw new ArgumentNullException ("member"); 108 if (owningObject == null) 109 throw new ArgumentNullException ("owningObject"); 110 if (manager == null) 111 throw new ArgumentNullException ("manager"); 112 113 CodeStatementCollection statements = new CodeStatementCollection (); 114 115 CodeExpression expression = base.GetExpression (manager, owningObject); 116 if (expression == null) { 117 string name = manager.GetName (owningObject); 118 if (name == null) 119 name = base.GetUniqueName (manager, owningObject); 120 expression = new CodeVariableReferenceExpression (name); 121 base.SetExpression (manager, owningObject, expression); 122 } 123 124 if (member is PropertyDescriptor) 125 base.SerializeProperty (manager, statements, owningObject, (PropertyDescriptor) member); 126 if (member is EventDescriptor) 127 base.SerializeEvent (manager, statements, owningObject, (EventDescriptor) member); 128 129 return statements; 130 } 131 SerializeMemberAbsolute(IDesignerSerializationManager manager, object owningObject, MemberDescriptor member)132 public virtual CodeStatementCollection SerializeMemberAbsolute (IDesignerSerializationManager manager, 133 object owningObject, MemberDescriptor member) 134 { 135 if (member == null) 136 throw new ArgumentNullException ("member"); 137 if (owningObject == null) 138 throw new ArgumentNullException ("owningObject"); 139 if (manager == null) 140 throw new ArgumentNullException ("manager"); 141 142 SerializeAbsoluteContext context = new SerializeAbsoluteContext (member); 143 manager.Context.Push (context); 144 CodeStatementCollection result = this.SerializeMember (manager, owningObject, member); 145 manager.Context.Pop (); 146 return result; 147 } 148 149 Deserialize(IDesignerSerializationManager manager, object codeObject)150 public virtual object Deserialize (IDesignerSerializationManager manager, object codeObject) 151 { 152 object deserialized = null; 153 154 CodeExpression expression = codeObject as CodeExpression; 155 if (expression != null) 156 deserialized = base.DeserializeExpression (manager, null, expression); 157 158 CodeStatement statement = codeObject as CodeStatement; 159 if (statement != null) 160 deserialized = DeserializeStatementToInstance (manager, statement); 161 162 CodeStatementCollection statements = codeObject as CodeStatementCollection; 163 if (statements != null) { 164 foreach (CodeStatement s in statements) { 165 if (deserialized == null) 166 deserialized = DeserializeStatementToInstance (manager, s); 167 else 168 DeserializeStatement (manager, s); 169 } 170 } 171 return deserialized; 172 } 173 DeserializeStatementToInstance(IDesignerSerializationManager manager, CodeStatement statement)174 protected object DeserializeStatementToInstance (IDesignerSerializationManager manager, CodeStatement statement) 175 { 176 CodeAssignStatement assignment = statement as CodeAssignStatement; 177 if (assignment != null) { 178 // CodeFieldReferenceExpression 179 // 180 CodeFieldReferenceExpression fieldRef = assignment.Left as CodeFieldReferenceExpression; 181 if (fieldRef != null) 182 return base.DeserializeExpression (manager, fieldRef.FieldName, assignment.Right); 183 } 184 base.DeserializeStatement (manager, statement); 185 return null; 186 } 187 } 188 } 189