Convert object to byte array and vice versa,
public static class ByteArrayConverter
{
public static byte[] Serialize<T>(T obj) where T : class
{
if (obj != null)
{
var ser = new XmlSerializer(typeof(T));
using (var ms = new MemoryStream())
{
ser.Serialize(ms, obj);
var bytes = ms.ToArray();
return bytes;
}
}
return null;
}
public static T Deserilize<T>(byte[] byteArray) where T : class
{
if (byteArray != null && byteArray.Length!=0)
{
var ser = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(byteArray))
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreWhitespace = false;
return (T)ser.Deserialize(XmlReader.Create(ms, readerSettings));
}
}
return null;
}
}
public static class ByteArrayConverter
{
public static byte[] Serialize<T>(T obj) where T : class
{
if (obj != null)
{
var ser = new XmlSerializer(typeof(T));
using (var ms = new MemoryStream())
{
ser.Serialize(ms, obj);
var bytes = ms.ToArray();
return bytes;
}
}
return null;
}
public static T Deserilize<T>(byte[] byteArray) where T : class
{
if (byteArray != null && byteArray.Length!=0)
{
var ser = new XmlSerializer(typeof(T));
using (MemoryStream ms = new MemoryStream(byteArray))
{
XmlReaderSettings readerSettings = new XmlReaderSettings();
readerSettings.IgnoreWhitespace = false;
return (T)ser.Deserialize(XmlReader.Create(ms, readerSettings));
}
}
return null;
}
}
No comments:
Post a Comment