-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStreamConvert.cs
More file actions
39 lines (35 loc) · 1.05 KB
/
StreamConvert.cs
File metadata and controls
39 lines (35 loc) · 1.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace Cmd.Logging.Tools
{
public static class StreamConverter
{
private readonly static BinaryFormatter BinaryFormatter = new BinaryFormatter();
public static Stream Serialize<T>(T objToSerialize)
{
Stream serializationStream = new MemoryStream();
try
{
BinaryFormatter.Serialize(serializationStream, objToSerialize);
}
catch (Exception)
{
serializationStream = null;
}
return serializationStream;
}
public static T Deserialize<T>(Stream streamToDeserialize) where T : class
{
try
{
var objectToReturn = BinaryFormatter.Deserialize(streamToDeserialize);
return objectToReturn as T;
}
catch (Exception)
{
return null;
}
}
}
}