andrew4582

Serializable Object Wrapper

Nov 22nd, 2013
328
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 2.72 KB | None | 0 0
  1. using System;
  2. using System.Runtime.Serialization;
  3.  
  4. namespace Common.Caching
  5. {
  6.     /// <summary>Wraps objects that aren't marked as serializable and uses Xml serialization to serialize and deserialize them</summary>
  7.     [Serializable]
  8.     public class SerializableObjectWrapper : ISerializable
  9.     {
  10.         [NonSerialized]
  11.         object obj;
  12.  
  13.         string objxml;
  14.         Type objType;
  15.  
  16.         public Type ObjectType
  17.         {
  18.             get { return objType; }
  19.         }
  20.  
  21.         protected SerializableObjectWrapper(SerializationInfo info, StreamingContext context)
  22.         {
  23.             objxml = (string)info.GetValue("objxml", typeof(string));
  24.             objType = (Type)info.GetValue("objType", typeof(Type));
  25.         }
  26.  
  27.         public SerializableObjectWrapper(object core)
  28.         {
  29.             this.obj = core;
  30.             if (core != null)
  31.                 this.objType = core.GetType();
  32.         }
  33.  
  34.         public object GetValue()
  35.         {
  36.             if (obj == null)
  37.             {
  38.                 if (string.IsNullOrWhiteSpace(objxml))
  39.                     return null;
  40.  
  41.                 Type obtype = this.ObjectType;
  42.                 if (obtype == null)
  43.                     obtype = typeof(object);
  44.                 obj = Deserialize(objxml, obtype);
  45.             }
  46.             return obj;
  47.         }
  48.  
  49.         public void GetObjectData(SerializationInfo info, StreamingContext context)
  50.         {
  51.             info.AddValue("objxml", Serialize(obj));
  52.             info.AddValue("objType", objType);
  53.         }
  54.  
  55.         static string Serialize(object xmlObject, bool encodeInBase64 = false)
  56.         {
  57.             using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream())
  58.             {
  59.                 System.Xml.Serialization.XmlSerializer xs = new System.Xml.Serialization.XmlSerializer(xmlObject.GetType());
  60.                 System.Xml.Serialization.XmlSerializerNamespaces xmlNamespace = new System.Xml.Serialization.XmlSerializerNamespaces();
  61.                 xmlNamespace.Add(string.Empty, string.Empty);
  62.                 xs.Serialize(memoryStream, xmlObject, xmlNamespace);
  63.                 System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  64.                 if (encodeInBase64)
  65.                     return Convert.ToBase64String(System.Text.ASCIIEncoding.Unicode.GetBytes(encoding.GetString(memoryStream.ToArray())));
  66.                 else
  67.                     return encoding.GetString(memoryStream.ToArray());
  68.             }
  69.         }
  70.  
  71.         static object Deserialize(string xml, Type objtype)
  72.         {
  73.             bool decodeInBase64 = false;
  74.             string data = xml;
  75.             if (decodeInBase64)
  76.                 data = System.Text.ASCIIEncoding.Unicode.GetString(Convert.FromBase64String(xml));
  77.             System.Xml.Serialization.XmlSerializer xs = System.Xml.Serialization.XmlSerializer.FromTypes(new Type[] { objtype })[0];
  78.             System.Text.UTF8Encoding encoding = new System.Text.UTF8Encoding();
  79.             using (System.IO.MemoryStream memoryStream = new System.IO.MemoryStream(encoding.GetBytes(data)))
  80.             {
  81.                 System.Xml.XmlTextWriter xmlTextWriter = new System.Xml.XmlTextWriter(memoryStream, System.Text.Encoding.UTF8);
  82.                 return xs.Deserialize(memoryStream);
  83.             }
  84.         }
  85.     }
  86. }
Advertisement
Add Comment
Please, Sign In to add comment