-
Notifications
You must be signed in to change notification settings - Fork 0
/
IoFile.cs
108 lines (99 loc) · 3.96 KB
/
IoFile.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
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
using System;
using System.IO;
using IoDeSer.DeSer;
namespace IoDeSer
{
/// <summary>
/// Class managing read/write conversion of .io file format.
/// </summary>
public static class IoFile
{
/*
*
* Writers
* Object -> TO
*
*/
/// <summary>
/// Converts the provided object <paramref name="obj"/> to .io file format.
/// <para>
/// ---</para>
/// <para>
/// Throws <see cref="ArgumentNullException"/> if the object <paramref name="obj"/> or some of its components are null.
/// </para>
/// <para>
/// Throws <see cref="NotSupportedException"/> if the object's type is not supported.
/// </para>
/// </summary>
/// <returns>String in .io file format.</returns>
public static string WriteToString<T>(T obj)
{
return IoSer.WriteToString(obj);
}
/// <summary>
/// Converts the provided object <paramref name="obj"/> to .io file format and writes it to the stream <paramref name="sw"/>.
/// <para>
/// ---</para>
/// <para>
/// Throws <see cref="ArgumentNullException"/> if the object <paramref name="obj"/> or some of its components are null.
/// </para>
/// <para>
/// Throws <see cref="NotSupportedException"/> if the object's type is not supported.
/// </para>
/// </summary>
public static void WriteToFile<T>(T obj, StreamWriter sw)
{
sw.Write(WriteToString(obj));
}
/*
*
* Readers
* Object <- FROM
*
*/
/// <summary>
/// Reads the content of .io file as stream <paramref name="sr"/> and tries to convert it to type <typeparamref name="T"/>.
/// <para>
/// ---
/// </para>
/// <para>
/// Throws <see cref="InvalidCastException"/> if the provided type <typeparamref name="T"/> does not contain some property from the file.
/// </para>
/// <para>
/// Throws <see cref="InvalidDataException"/> when the type <typeparamref name="T"/> is not supported.
/// </para>
/// <para>
/// Throws <see cref="MissingMethodException"/> when the type <typeparamref name="T"/> does not implement parameterless constructor.
/// </para>
/// </summary>
/// <param name="sr">Stream to .io file.</param>
/// <typeparam name="T">The type of object that will be converted from stream <paramref name="sr"/>.</typeparam>
/// <returns>Object of type <typeparamref name="T"/>.</returns>
public static T ReadFromFile<T>(StreamReader sr)
{
return ReadFromString<T>(sr.ReadToEnd().Trim());
}
/// <summary>
/// Reads content of string <paramref name="ioString"/> in .io file format and tries to convert it to type <typeparamref name="T"/>.
/// <para>
/// ---
/// </para>
/// <para>
/// Throws <see cref="InvalidCastException"/> if the provided type <typeparamref name="T"/> does not contain some property from the string <paramref name="ioString"/>.
/// </para>
/// <para>
/// Throws <see cref="InvalidDataException"/> when the type <typeparamref name="T"/> is not supported.
/// </para>
/// <para>
/// Throws <see cref="MissingMethodException"/> when the type <typeparamref name="T"/> does not implement parameterless constructor.
/// </para>
/// </summary>
/// <param name="ioString">String in .io file format.</param>
/// <typeparam name="T">The type of object that will be converted from string <paramref name="ioString"/>.</typeparam>
/// <returns>Object of type <typeparamref name="T"/>.</returns>
public static T ReadFromString<T>(string ioString)
{
return (T)IoDes.ReadFromString(ioString, typeof(T));
}
}
}