Get an object size

Saratvaddilli
Posted by Saratvaddilli under C# category on | Points: 40 | Views : 872
            public static int GetObjSize(Object obj)
{
try
{
string XmlString = string.Empty;
MemoryStream MemStream = new MemoryStream();
XmlSerializer Serializer = new XmlSerializer(obj.GetType());
XmlTextWriter XmlText = new XmlTextWriter(MemStream, Encoding.Default);
Serializer.Serialize(XmlText, obj);
byte[] bytes = new byte[] { };
bytes = MemStream.ToArray();
XmlString = Encoding.Default.GetString(bytes, 0, bytes.Length);
MemStream.Flush();
MemStream.Close();
XmlText.Flush();
XmlText.Close();

byte[] bytesobj = new byte[XmlString.Length * sizeof(char)];
Buffer.BlockCopy(XmlString.ToCharArray(), 0, bytes, 0, bytes.Length);
return bytesobj.Length;
}
catch (Exception e)
{
return -1;
}

Comments or Responses

Login to post response