Object To Byte Array - CSharp System

CSharp examples for System:Byte Array

Description

Object To Byte Array

Demo Code


using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
using System.IO;//from w ww  .  ja  va  2 s  . c o  m
using System.Collections.Generic;
using System;

public class Main{
        // Convert an object to a byte array
        public static byte[] ObjectToByteArray(Object obj)
        {
            if (obj == null)
                return null;
            var bf = new BinaryFormatter();
            MemoryStream ms = new MemoryStream();
            bf.Serialize(ms, obj);
            return ms.ToArray();
        }
}

Related Tutorials