CSLA.NET 6.0.0
CSLA .NET is a software development framework that helps you build a reusable, maintainable object-oriented business layer for your app.
SerializationInfo.cs
Go to the documentation of this file.
1//-----------------------------------------------------------------------
2// <copyright file="SerializationInfo.cs" company="Marimer LLC">
3// Copyright (c) Marimer LLC. All rights reserved.
4// Website: https://cslanet.com
5// </copyright>
6// <summary>Object containing the serialization</summary>
7//-----------------------------------------------------------------------
8using System;
9using System.Collections.Generic;
10using System.Linq;
11using System.Text;
12using System.Xml.Linq;
13using System.Runtime.Serialization;
14
16{
21#if TESTING
22 [System.Diagnostics.DebuggerNonUserCode]
23#endif
25 [DataContract]
27 {
33 [DataContract]
34 public class FieldData : IMobileObject
35 {
39 [DataMember]
40 public object Value { get; set; }
41
47 [DataMember]
48 public string EnumTypeName { get; set; }
49
53 [DataMember]
54 public bool IsDirty { get; set; }
55
56 #region IMobileObject Members
57
61 public FieldData() { }
62
67 public void GetState(SerializationInfo info)
68 {
69 info.AddValue("FieldData.Value", Value);
70 info.AddValue("FieldData.EnumTypeName", EnumTypeName);
71 info.AddValue("FieldData.IsDirty", IsDirty);
72 }
73
79 public void GetChildren(SerializationInfo info, MobileFormatter formatter)
80 {
81 }
82
87 public void SetState(SerializationInfo info)
88 {
89 Value = info.GetValue<object>("FieldData.Value");
90 EnumTypeName = info.GetValue<string>("FieldData.EnumTypeName");
91 IsDirty = info.GetValue<bool>("FieldData.IsDirty");
92 }
93
99 public void SetChildren(SerializationInfo info, MobileFormatter formatter)
100 {
101 }
102
103 #endregion
104 }
105
111 [DataContract]
113 {
117 [DataMember]
118 public int ReferenceId { get; set; }
122 [DataMember]
123 public bool IsDirty { get; set; }
124
125 #region IMobileObject Members
126
130 public ChildData() { }
131
140 public void GetState(SerializationInfo info)
141 {
142 info.AddValue("ChildData.ReferenceId", ReferenceId);
143 info.AddValue("ChildData.IsDirty", IsDirty);
144 }
145
157 public void GetChildren(SerializationInfo info, MobileFormatter formatter)
158 {
159 }
160
169 public void SetState(SerializationInfo info)
170 {
171 ReferenceId = info.GetValue<int>("ChildData.ReferenceId");
172 IsDirty = info.GetValue<bool>("ChildData.IsDirty");
173 }
174
186 public void SetChildren(SerializationInfo info, MobileFormatter formatter)
187 {
188 }
189
190 #endregion
191 }
192
193 private Dictionary<string, ChildData> _children = new Dictionary<string, ChildData>();
197 [DataMember()]
198 public Dictionary<string, ChildData> Children
199 {
200 get { return _children; }
201 set { _children = value; }
202 }
203
204 private Dictionary<string, FieldData> _values = new Dictionary<string, FieldData>();
208 [DataMember()]
209 public Dictionary<string, FieldData> Values
210 {
211 get { return _values; }
212 set { _values = value; }
213 }
214
215 internal SerializationInfo(int referenceId)
216 {
217 this.ReferenceId = referenceId;
218 }
219
223 [DataMember]
224 public int ReferenceId { get; set; }
229 [DataMember]
230 public string TypeName { get; set; }
231
241 public void AddValue(string name, object value)
242 {
243 AddValue(name, value, false);
244 }
245
258 public void AddValue(string name, object value, bool isDirty)
259 {
260 _values.Add(name, new FieldData { Value = value, IsDirty = isDirty });
261 }
262
278 public void AddValue(string name, object value, bool isDirty, string enumTypeName)
279 {
280 _values.Add(name, new FieldData { Value = value, IsDirty = isDirty, EnumTypeName = enumTypeName});
281 }
282
293 public T GetValue<T>(string name)
294 {
295 try
296 {
297 var value = _values[name].Value;
298 return (value != null ? (T)Utilities.CoerceValue<T>(value.GetType(), null, value) : (T)value);
299 }
300 catch (Exception ex)
301 {
302 throw new InvalidOperationException(string.Format("SerializationInfo.GetValue: {0}", name), ex);
303 }
304 }
305
315 public void AddChild(string name, int referenceId)
316 {
317 AddChild(name, referenceId, false);
318 }
319
332 public void AddChild(string name, int referenceId, bool isDirty)
333 {
334 _children.Add(name, new ChildData { ReferenceId = referenceId, IsDirty = isDirty });
335 }
336
337 #region IMobileObject Members
338
342 public SerializationInfo() { }
343
352 public void GetState(SerializationInfo info)
353 {
354 info.AddValue("SerializationInfo.ReferenceId", ReferenceId);
355 info.AddValue("SerializationInfo.TypeName", TypeName);
356 }
357
369 public void GetChildren(SerializationInfo info, MobileFormatter formatter)
370 {
371 foreach (string key in _children.Keys)
372 {
373 ChildData value = _children[key];
374 SerializationInfo si = formatter.SerializeObject(value);
375 info.AddChild(key, si.ReferenceId);
376 }
377 foreach (string key in _values.Keys)
378 {
379 FieldData value = _values[key];
380 SerializationInfo si = formatter.SerializeObject(value);
381 info.AddChild(key, si.ReferenceId);
382 }
383 }
384
393 public void SetState(SerializationInfo info)
394 {
395 ReferenceId = info.GetValue<int>("SerializationInfo.ReferenceId");
396 TypeName = info.GetValue<string>("SerializationInfo.TypeName");
397 }
398
410 public void SetChildren(SerializationInfo info, MobileFormatter formatter)
411 {
412 foreach (string key in info.Children.Keys)
413 {
414 int referenceId = info.Children[key].ReferenceId;
415 object serialized = formatter.GetObject(referenceId);
416 if (serialized is ChildData)
417 {
418 _children.Add(key, (ChildData)serialized);
419 }
420 else
421 {
422 _values.Add(key, (FieldData)serialized);
423 }
424 }
425 }
426
427 #endregion
428 }
429}
Serializes and deserializes objects at the field level.
Object that contains information about a single child reference.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
bool IsDirty
Indicates whether the child is dirty.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
Object that contains information about a single field.
void SetState(SerializationInfo info)
Sets state information.
string EnumTypeName
If non-null, indicates that the value is a integer value representing the specified enum type.
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Gets child serialization information.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Sets child serialization information.
void GetState(SerializationInfo info)
Gets state information.
bool IsDirty
Indicates whether the field is dirty.
Object containing the serialization data for a specific object.
Dictionary< string, FieldData > Values
Dictionary containg field data.
void GetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should serialize its child references.
int ReferenceId
Reference number for this object.
string TypeName
Assembly-qualified type name of the object being serialized.
void AddValue(string name, object value, bool isDirty, string enumTypeName)
Adds a value to the list of fields.
void GetState(SerializationInfo info)
Method called by MobileFormatter when an object should serialize its data.
Dictionary< string, ChildData > Children
Dictionary containing child reference data.
SerializationInfo()
Creates an instance of the type.
void SetState(SerializationInfo info)
Method called by MobileFormatter when an object should be deserialized.
void AddChild(string name, int referenceId)
Adds a child to the list of child references.
void AddValue(string name, object value)
Adds a value to the serialization stream.
T GetValue< T >(string name)
Gets a value from the list of fields.
void AddChild(string name, int referenceId, bool isDirty)
Adds a child to the list of child references.
void AddValue(string name, object value, bool isDirty)
Adds a value to the list of fields.
void SetChildren(SerializationInfo info, MobileFormatter formatter)
Method called by MobileFormatter when an object should deserialize its child references.
Interface to be implemented by any object that supports serialization by the SerializationFormatterFa...
@ Serializable
Prevents updating or inserting until the transaction is complete.