protected virtual void Deserialize(SerializationInfo info, XmlFormatter formatter) { var currentType = GetType(); while (currentType != null) { // get the list of fields in this type var fields = currentType.GetFields( BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); foreach (var field in fields) { // see if this field is marked as not undoable if (field.IsNotSerialized || IsNonSerialized(field)) { continue; } var value = info.GetValue(string.Format("{0}!{1}", field.DeclaringType.Name, field.Name)); var valueInfo = value as SerializationInfo; if (valueInfo == null) { SetValue(field, Convert.ChangeType(value, field.FieldType, null)); } else { SetValue(field, formatter.GetObject(valueInfo.ReferenceId)); } } currentType = currentType.BaseType; } }
protected virtual void Serialize(SerializationInfo info, XmlFormatter formatter) { var currentType = GetType(); while (currentType != null) { var fields = currentType.GetFields( BindingFlags.NonPublic | BindingFlags.Instance | BindingFlags.Public); foreach (var field in fields) { if (field.IsNotSerialized || IsNonSerialized(field)) { continue; } var value = GetValue(field); var mobile = value as ISerializable; info.AddValue( string.Format("{0}!{1}", field.DeclaringType.Name, field.Name), mobile == null ? value : formatter.SerializeObject(mobile)); } currentType = currentType.BaseType; } }
void ISerializable.Serialize(SerializationInfo info, XmlFormatter formatter) { var thisType = GetType(); info.TypeName = string.Format("{0},{1}", thisType.FullName, thisType.Assembly.FullName); Serialize(info, formatter); }
internal void Deserialize(XElement data, XmlFormatter formatter) { foreach (var item in data.Elements()) { if (item.Name == "f") { var entry = new ValueEntry(item.Attribute("n").Value, item.Attribute("v").Value); _values.Add(entry.Name, entry); } else if (item.Name == "l") { var listItems = item.Elements().Select(content => new SerializationInfo(content)).ToList(); var entry = new ValueEntry(item.Attribute("n").Value, listItems); _values.Add(entry.Name, entry); } else { var referenceId = Convert.ToInt32(item.Attribute("i").Value); var entry = new ValueEntry( item.Attribute("n").Value, new SerializationInfo(referenceId)); _values.Add(entry.Name, entry); } } }
void ISerializable.Deserialize(SerializationInfo info, XmlFormatter formatter) { Deserialize(info, formatter); }
private ISerializable UnZipZeroLength(byte[] zipped) { if (zipped == null) { return null; } object result = null; var formatter = new XmlFormatter(); var memStream = new MemoryStream(zipped); using (var zipStream = new ZipInputStream(memStream)) { var zipEntry = zipStream.GetNextEntry(); if (zipEntry != null) { result = formatter.Deserialize(zipStream); } zipStream.Close(); } memStream.Close(); return (ISerializable)result; }
private byte[] ZipZeroLength(ISerializable data) { var formatter = new XmlFormatter(); var memStream = new MemoryStream(); using (var zipStream = new ZipOutputStream(memStream)) { zipStream.PutNextEntry(new ZipEntry("data")); formatter.Serialize(zipStream, data); zipStream.CloseEntry(); zipStream.Close(); } var result = memStream.ToArray(); memStream.Close(); return result; }