/
Invariants.cs
131 lines (120 loc) · 4.36 KB
/
Invariants.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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
namespace Serenity;
/// <summary>
/// Invariant culture related constants and formats.
/// </summary>
public static class Invariants
{
/// <summary>
/// Number format information for invariant culture</summary>
public static readonly NumberFormatInfo NumberFormat;
/// <summary>
/// Date time format information for invariant culture</summary>
public static readonly DateTimeFormatInfo DateTimeFormat;
/// <summary>
/// Constructor of the static DataHelper. Initializes default connection string and connection culture.
/// </summary>
static Invariants()
{
NumberFormat = NumberFormatInfo.InvariantInfo;
DateTimeFormat = DateTimeFormatInfo.InvariantInfo;
}
/// <summary>
/// Determines whether type of the value is an integer type (<see cref="short"/>, <see cref="int"/>, <see cref="long"/>).
/// Avoid using this function as it is obsolete.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>
/// <c>true</c> if integer type; otherwise, <c>false</c>.
/// </returns>
public static bool IsIntegerType(object value)
{
return value != null &&
(value is int ||
value is long ||
value is short);
}
/// <summary>
/// Converts value to string using invariant culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns></returns>
public static string ToInvariant(this int value)
{
return value.ToString(NumberFormat);
}
/// <summary>
/// Converts value to string using invariant culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Converted string.</returns>
public static string ToInvariant(this long value)
{
return value.ToString(NumberFormat);
}
/// <summary>
/// Converts value to string using invariant culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Converted string.</returns>
public static string ToInvariant(this double value)
{
return value.ToString(NumberFormat);
}
/// <summary>
/// Converts value to string using invariant culture.
/// </summary>
/// <param name="value">The value.</param>
/// <returns>Converted string.</returns>
public static string ToInvariant(this decimal value)
{
return value.ToString(NumberFormat);
}
/// <summary>
/// <p>Tries to converts an ID's string representation to its numerical ID value (<see cref="long"/>).</p>
/// <p>Unlike <see cref="long.Parse(string)"/>, <c>null</c>, empty string and all other
/// invalid strings results in <see cref="long"/> value (not an exception).</p></summary>
/// <param name="str">
/// String representation of an ID.</param>
/// <returns>
/// Numerical ID value or Null.Int64 if null, empty, or invalid string.</returns>
/// <seealso cref="TryParseID(string)"/>
/// <seealso cref="long.Parse(string)"/>
public static long? TryParseID(this string? str)
{
if (long.TryParse(str, out long id))
return id;
else
return null;
}
/// <summary>
/// <p>Tries to converts an ID's string representation to its numerical ID value (Int64).</p>
/// <p>Unlike <see cref="long.Parse(string)"/>, <c>null</c>, empty string and all other
/// invalid strings results in <see cref="long"/> value (not an exception).</p></summary>
/// <param name="str">
/// String representation of an ID.</param>
/// <returns>
/// Numerical ID value or Null.Int64 if null, empty, or invalid string.</returns>
/// <seealso cref="TryParseID(string)"/>
/// <seealso cref="long.Parse(string)"/>
public static int? TryParseID32(this string? str)
{
if (int.TryParse(str, out int id))
return id;
else
return null;
}
/// <summary>
/// Converts an ID value, to its string representation.</summary>
/// <param name="id">
/// ID value.</param>
/// <returns>
/// If <paramref name="id"/> has <see cref="long"/> value, <c><see cref="string.Empty"/></c>,
/// otherwise its string representation</returns>
public static string IDString(this long? id)
{
if (id == null)
return string.Empty;
else
return id.ToString();
}
}