-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathBinaryFileUtility.cs
More file actions
34 lines (31 loc) · 1015 Bytes
/
BinaryFileUtility.cs
File metadata and controls
34 lines (31 loc) · 1015 Bytes
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
using System.IO;
using System.Runtime.CompilerServices;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace OlegHcp.IO
{
public class BinaryFileUtility
{
[MethodImpl(MethodImplOptions.AggressiveInlining)]
public static T Load<T>(string path)
{
return (T)Load(path);
}
public static object Load(string path)
{
IFormatter formatter = new BinaryFormatter();
using (Stream stream = new FileStream(path, FileMode.Open, FileAccess.Read, FileShare.Read))
{
return formatter.Deserialize(stream);
}
}
public static void Save(string path, object obj)
{
IFormatter formatter = new BinaryFormatter();
using (Stream stream = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None))
{
formatter.Serialize(stream, obj);
}
}
}
}