11using System.CodeDom.Compiler;
12using System.Collections.Generic;
24 internal class SerializationPartialBuilder
33 internal GenerationResults BuildPartialTypeDefinition(ExtractedTypeDefinition typeDefinition)
35 GenerationResults generationResults;
36 IndentedTextWriter textWriter;
38 using (StringWriter stringWriter =
new StringWriter())
40 textWriter =
new IndentedTextWriter(stringWriter,
"\t");
42 AppendUsingStatements(textWriter, typeDefinition);
43 AppendContainerDefinitions(textWriter, typeDefinition);
45 AppendTypeDefinition(textWriter, typeDefinition);
46 AppendBlockStart(textWriter);
48 AppendGetChildrenMethod(textWriter, typeDefinition);
49 AppendGetStateMethod(textWriter, typeDefinition);
50 AppendSetChildrenMethod(textWriter, typeDefinition);
51 AppendSetStateMethod(textWriter, typeDefinition);
53 AppendBlockEnd(textWriter);
55 AppendContainerDefinitionClosures(textWriter, typeDefinition);
57 generationResults =
new GenerationResults()
59 FullyQualifiedName = typeDefinition.FullyQualifiedName,
60 GeneratedSource = stringWriter.ToString()
64 return generationResults;
67 #region Private Helper Methods
73 private void AppendBlockStart(IndentedTextWriter textWriter)
75 textWriter.WriteLine(
"{");
83 private void AppendBlockEnd(IndentedTextWriter textWriter)
86 textWriter.WriteLine(
"}");
95 private void AppendUsingStatements(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
97 HashSet<string> requiredNamespaces;
99 requiredNamespaces = GetRequiredNamespaces(typeDefinition);
101 foreach (
string requiredNamespace
in requiredNamespaces)
103 textWriter.Write(
"using ");
104 textWriter.Write(requiredNamespace);
105 textWriter.WriteLine(
";");
108 textWriter.WriteLine();
116 private HashSet<string> GetRequiredNamespaces(ExtractedTypeDefinition typeDefinition)
118 HashSet<string> requiredNamespaces =
new HashSet<string>() {
"System",
"Csla.Serialization.Mobile" };
120 foreach (ExtractedFieldDefinition fieldDefinition
in typeDefinition.Fields)
122 requiredNamespaces.Add(fieldDefinition.TypeDefinition.TypeNamespace);
125 foreach (ExtractedPropertyDefinition propertyDefinition
in typeDefinition.Properties)
127 requiredNamespaces.Add(propertyDefinition.TypeDefinition.TypeNamespace);
130 requiredNamespaces.Remove(typeDefinition.Namespace);
132 return requiredNamespaces;
140 private void AppendContainerDefinitions(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
142 foreach (ExtractedContainerDefinition containerDefinition
in typeDefinition.ContainerDefinitions)
144 textWriter.WriteLine(containerDefinition.FullDefinition);
145 AppendBlockStart(textWriter);
154 private void AppendContainerDefinitionClosures(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
156 foreach (ExtractedContainerDefinition containerDefinition
in typeDefinition.ContainerDefinitions)
158 AppendBlockEnd(textWriter);
167 private void AppendTypeDefinition(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
169 textWriter.WriteLine(
"[Serializable]");
170 textWriter.Write(typeDefinition.Scope);
171 textWriter.Write(
" partial ");
172 textWriter.Write(typeDefinition.TypeKind);
173 textWriter.Write(
" ");
174 textWriter.Write(typeDefinition.TypeName);
175 textWriter.Write(
" : IMobileObject");
176 textWriter.WriteLine();
184 private void AppendGetChildrenMethod(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
186 textWriter.WriteLine(
"void IMobileObject.GetChildren(SerializationInfo info, MobileFormatter formatter)");
187 AppendBlockStart(textWriter);
188 if (HasChildrenToExpose(typeDefinition))
190 textWriter.WriteLine(
"IMobileObject mobileObject;");
191 textWriter.WriteLine(
"SerializationInfo childInfo;");
192 textWriter.WriteLine();
195 foreach (ExtractedFieldDefinition fieldDefinition
in typeDefinition.Fields)
197 if (!IsChildToExpose(fieldDefinition))
continue;
199 AppendSerializeChildFragment(textWriter, fieldDefinition);
202 foreach (ExtractedPropertyDefinition propertyDefinition
in typeDefinition.Properties)
204 if (!IsChildToExpose(propertyDefinition))
continue;
206 AppendSerializeChildFragment(textWriter, propertyDefinition);
209 AppendBlockEnd(textWriter);
210 textWriter.WriteLine();
218 private void AppendSerializeChildFragment(IndentedTextWriter textWriter, IMemberDefinition memberDefinition)
220 textWriter.Write(
"mobileObject = ");
221 textWriter.Write(memberDefinition.MemberName);
222 textWriter.WriteLine(
" as IMobileObject;");
223 textWriter.WriteLine(
"childInfo = formatter.SerializeObject(mobileObject);");
224 textWriter.Write(
"info.AddChild(nameof(");
225 textWriter.Write(memberDefinition.MemberName);
226 textWriter.WriteLine(
"), childInfo.ReferenceId, true);");
234 private void AppendSetChildrenMethod(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
236 textWriter.WriteLine(
"void IMobileObject.SetChildren(SerializationInfo info, MobileFormatter formatter)");
237 AppendBlockStart(textWriter);
239 if (HasChildrenToExpose(typeDefinition))
241 textWriter.WriteLine(
"SerializationInfo.ChildData childData;");
242 textWriter.WriteLine();
245 foreach (ExtractedFieldDefinition fieldDefinition
in typeDefinition.Fields)
247 if (!IsChildToExpose(fieldDefinition))
continue;
249 AppendDeserializeChildFragment(textWriter, fieldDefinition);
252 foreach (ExtractedPropertyDefinition propertyDefinition
in typeDefinition.Properties)
254 if (!IsChildToExpose(propertyDefinition))
continue;
256 AppendDeserializeChildFragment(textWriter, propertyDefinition);
259 AppendBlockEnd(textWriter);
260 textWriter.WriteLine();
268 private void AppendDeserializeChildFragment(IndentedTextWriter textWriter, IMemberDefinition memberDefinition)
270 textWriter.Write(
"if (info.Children.ContainsKey(nameof(");
271 textWriter.Write(memberDefinition.MemberName);
272 textWriter.WriteLine(
")))");
273 AppendBlockStart(textWriter);
275 textWriter.Write(
"childData = info.Children[nameof(");
276 textWriter.Write(memberDefinition.MemberName);
277 textWriter.WriteLine(
")];");
278 textWriter.WriteLine();
279 textWriter.Write(memberDefinition.MemberName);
280 textWriter.Write(
" = formatter.GetObject(childData.ReferenceId) as ");
281 textWriter.Write(memberDefinition.TypeDefinition.TypeName);
282 textWriter.WriteLine(
";");
284 AppendBlockEnd(textWriter);
292 private void AppendGetStateMethod(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
294 textWriter.WriteLine(
"void IMobileObject.GetState(SerializationInfo info)");
295 AppendBlockStart(textWriter);
297 foreach (ExtractedFieldDefinition fieldDefinition
in typeDefinition.Fields)
299 if (IsChildToExpose(fieldDefinition))
continue;
301 AppendGetMemberStateFragment(textWriter, fieldDefinition);
304 foreach (ExtractedPropertyDefinition propertyDefinition
in typeDefinition.Properties)
306 if (IsChildToExpose(propertyDefinition))
continue;
308 AppendGetMemberStateFragment(textWriter, propertyDefinition);
311 AppendBlockEnd(textWriter);
312 textWriter.WriteLine();
320 private void AppendGetMemberStateFragment(IndentedTextWriter textWriter, IMemberDefinition memberDefinition)
322 textWriter.Write(
"info.AddValue(nameof(");
323 textWriter.Write(memberDefinition.MemberName);
324 textWriter.Write(
"), ");
325 textWriter.Write(memberDefinition.MemberName);
326 textWriter.WriteLine(
");");
334 private void AppendSetStateMethod(IndentedTextWriter textWriter, ExtractedTypeDefinition typeDefinition)
336 textWriter.WriteLine(
"void IMobileObject.SetState(SerializationInfo info)");
337 AppendBlockStart(textWriter);
339 foreach (ExtractedFieldDefinition fieldDefinition
in typeDefinition.Fields)
341 if (IsChildToExpose(fieldDefinition))
continue;
343 AppendSetMemberStateMethod(textWriter, fieldDefinition);
346 foreach (ExtractedPropertyDefinition propertyDefinition
in typeDefinition.Properties)
348 if (IsChildToExpose(propertyDefinition))
continue;
350 AppendSetMemberStateMethod(textWriter, propertyDefinition);
353 AppendBlockEnd(textWriter);
354 textWriter.WriteLine();
362 private void AppendSetMemberStateMethod(IndentedTextWriter textWriter, IMemberDefinition memberDefinition)
364 textWriter.Write(memberDefinition.MemberName);
365 textWriter.Write(
" = info.GetValue<");
366 textWriter.Write(memberDefinition.TypeDefinition.TypeName);
367 textWriter.Write(
">(nameof(");
368 textWriter.Write(memberDefinition.MemberName);
369 textWriter.WriteLine(
"));");
377 private bool HasChildrenToExpose(ExtractedTypeDefinition typeDefinition)
379 return typeDefinition.Properties.Any(p => IsChildToExpose(p)) ||
380 typeDefinition.Fields.Any(f => IsChildToExpose(f));
388 private bool IsChildToExpose(IMemberDefinition memberDefinition)
390 return memberDefinition.TypeDefinition.IsAutoSerializable || memberDefinition.TypeDefinition.ImplementsIMobileObject;