-
Notifications
You must be signed in to change notification settings - Fork 0
/
Extension.cs
60 lines (50 loc) · 1.74 KB
/
Extension.cs
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
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
using System;
using System.Collections.Generic;
using System.IO;
using System.Linq;
using System.Runtime.Serialization.Formatters.Binary;
using System.Text;
namespace MeFastTextBox
{
static class Extension
{
/// <summary>
/// десериализация класса
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="obj"></param>
/// <returns></returns>
public static T Deserialize<T>(this T obj) where T : class, new()
{
if (obj == null) obj = new T();
string name = obj.GetType().Name + "cfg";
if (!File.Exists(name)) return obj;
using (FileStream fs = new FileStream(name, FileMode.Open))
{
obj = new BinaryFormatter().Deserialize(fs) as T;
}
return obj ?? new T();
}
public static void Serialize<T>(this T obj) where T : class, new()
{
string name = obj.GetType().Name + "cfg";
using (FileStream fs = new FileStream(name, FileMode.OpenOrCreate))
{
new BinaryFormatter().Serialize(fs, obj);
}
}
/// <summary>
/// возвращает подстроку из строки в указанном диапазоне
/// </summary>
/// <param name="obj"></param>
/// <param name="start"></param>
/// <param name="end"></param>
/// <returns></returns>
public static string GetRange(this string obj, int start, int end)
{
if (end >= obj.Length) end = obj.Length;// - 1;
if (end < start) start = end;
return obj.Substring(start, end - start);
}
}
}