-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathTypeExtensions.cs
148 lines (126 loc) · 5.49 KB
/
TypeExtensions.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
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
//-----------------------------------------------------------------------
// <copyright file="TypeExtensions.cs" company="Talegen, LLC">
// Free for the world to use.
// </copyright>
//-----------------------------------------------------------------------
namespace TalegenProjectNamiCacheLoader
{
using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.Cryptography;
/// <summary>
/// This class contains a number of extension methods for conversion of data values.
/// </summary>
public static class TypeExtensions
{
#region Private Constants
/// <summary>
/// Contains alpha-characters for password generation.
/// </summary>
private const string AlphanumericCharacters =
"ABCDEFGHIJKLMNOPQRSTUVWXYZ" +
"abcdefghijklmnopqrstuvwxyz" +
"0123456789";
#endregion
/// <summary>
/// This method is used to generate a random alpha-numeric string.
/// </summary>
/// <param name="length">Contains the length of the string.</param>
/// <param name="characterSet">Optionally, contains the set of characters used for character selection.</param>
/// <returns>Returns a string containing random characters.</returns>
public static string RandomAlphaString(int length, IEnumerable<char> characterSet = null)
{
char[] result;
if (length < 0 || length >= int.MaxValue)
{
length = 10;
}
if (characterSet == null)
{
characterSet = AlphanumericCharacters;
}
var characterArray = characterSet.Distinct().ToArray();
var bytes = new byte[length * 8];
using (RandomNumberGenerator rng = new RNGCryptoServiceProvider())
{
rng.GetBytes(bytes);
result = new char[length];
for (int i = 0; i < length; i++)
{
ulong value = BitConverter.ToUInt64(bytes, i * 8);
result[i] = characterArray[value % (uint)characterArray.Length];
}
}
return new string(result);
}
/// <summary>
/// Converts a string to an integer value returning default value for null or <see cref="DBNull"/> types.
/// </summary>
/// <param name="value">Contains the string value to convert.</param>
/// <param name="defaultValue">Contains the default value to return if value cannot be converted.</param>
/// <returns>Returns the converted or default integer value.</returns>
public static int ToInt(this string value, int defaultValue = 0)
{
if (string.IsNullOrEmpty(value) || !int.TryParse(value, out var result))
{
result = defaultValue;
}
return result;
}
/// <summary>
/// Converts an object to a string value returning default value on null or <see cref="DBNull"/> types.
/// </summary>
/// <param name="value">Contains the object value to convert.</param>
/// <param name="defaultValue">Contains the default value to return if value cannot be converted.</param>
/// <returns>Returns the converted or default string value.</returns>
public static string ConvertToString(this object value, string defaultValue = "")
{
string result = defaultValue;
if (value != null && value != DBNull.Value)
{
result = value.ToString();
if (string.IsNullOrEmpty(result))
{
result = defaultValue;
}
}
return result;
}
/// <summary>
/// This method is used to convert a string value to a boolean.
/// </summary>
/// <param name="value">Contains the value to convert.</param>
/// <param name="defaultValue">Contains a default value to return when the value is invalid.</param>
/// <returns>Returns the converted value or default value.</returns>
public static bool ToBoolean(this string value, bool defaultValue = false)
{
bool result = defaultValue;
string[] allowedPositives = { "T", "TRUE", "1", "Y", "YES", "O" };
if (!string.IsNullOrWhiteSpace(value))
{
result = allowedPositives.Contains(value.ToUpperInvariant());
}
return result;
}
/// <summary>
/// This method is used to recurse through an exception's inner exceptions and return a combined message string containing all error messages.
/// </summary>
/// <param name="ex">Contains the exception object to recurse.</param>
/// <param name="recursionLevel">Contains the indentation level of the recursive messages.</param>
/// <returns>Returns a string containing all related exception messages.</returns>
public static string RecurseMessages(this Exception ex, int recursionLevel = 0)
{
string message = ex?.Message + Environment.NewLine;
if (recursionLevel > 0)
{
message = new string('-', recursionLevel) + ">" + message;
}
if (ex?.InnerException != null)
{
message += ex.InnerException.RecurseMessages(++recursionLevel);
}
return message;
}
}
}