Skip to content

Commit 240f953

Browse files
Added better support for null string values to type conversion
1 parent 93a6cdf commit 240f953

File tree

2 files changed

+37
-9
lines changed

2 files changed

+37
-9
lines changed

src/DotNetToolkit.Repository/Extensions/StringExtensions.cs

Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
namespace DotNetToolkit.Repository.Extensions
22
{
33
using JetBrains.Annotations;
4+
using System;
45
using System.Text;
56
using Utility;
67

@@ -39,5 +40,29 @@ public static string ToSHA256([NotNull] this string value)
3940

4041
return sb.ToString();
4142
}
43+
44+
public static bool ToBoolean(this string value)
45+
{
46+
if (value == null || value.Trim().Length == 0)
47+
return false;
48+
49+
switch (value.ToLower())
50+
{
51+
case "true":
52+
return true;
53+
case "on":
54+
return true;
55+
case "1":
56+
return true;
57+
case "0":
58+
return false;
59+
case "false":
60+
return false;
61+
case "off":
62+
return false;
63+
default:
64+
throw new InvalidCastException($"Cannot cast '{value}' to boolean.");
65+
}
66+
}
4267
}
4368
}

src/DotNetToolkit.Repository/Extensions/TypeExtensions.cs

Lines changed: 12 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -105,26 +105,23 @@ public static object ConvertTo([NotNull] this Type type, [CanBeNull] string valu
105105
}
106106
else if (type == typeof(int))
107107
{
108-
Result = int.Parse(value, NumberStyles.Integer, CultureInfo.CurrentCulture);
108+
Result = Convert.ToInt32(value);
109109
}
110110
else if (type == typeof(byte))
111111
{
112112
Result = Convert.ToByte(value);
113113
}
114114
else if (type == typeof(decimal))
115115
{
116-
Result = decimal.Parse(value, NumberStyles.Any, CultureInfo.CurrentCulture);
116+
Result = Convert.ToDecimal(value);
117117
}
118118
else if (type == typeof(double))
119119
{
120-
Result = double.Parse(value, NumberStyles.Any, CultureInfo.CurrentCulture);
120+
Result = Convert.ToDouble(value);
121121
}
122122
else if (type == typeof(bool))
123123
{
124-
if (value.ToLower() == "true" || value.ToLower() == "on" || value == "1")
125-
Result = true;
126-
else
127-
Result = false;
124+
Result = value.ToBoolean();
128125
}
129126
else if (type == typeof(DateTime))
130127
{
@@ -145,7 +142,7 @@ public static object ConvertTo([NotNull] this Type type, [CanBeNull] string valu
145142
}
146143
else
147144
{
148-
throw new Exception("Type Conversion not handled in ConvertTo method.");
145+
throw new Exception($"Type conversion not handled in {nameof(ConvertTo)} method.");
149146
}
150147
#else
151148
return Convert.ChangeType(value, type);
@@ -155,7 +152,13 @@ public static object ConvertTo([NotNull] this Type type, [CanBeNull] string valu
155152
return Result;
156153
}
157154

158-
internal static object InvokeConstructor([NotNull] this Type type, [CanBeNull] Dictionary<string, string> keyValues)
155+
/// <summary>
156+
/// Creates a new instance of the specified type with a constructor that best matches the collection of specified parameters; otherwise, creates an instance of the specified type using that type's default constructor.
157+
/// </summary>
158+
/// <param name="type">The type.</param>
159+
/// <param name="keyValues">The key value parameters.</param>
160+
/// <returns>The new instance of the specified type.</returns>
161+
public static object InvokeConstructor([NotNull] this Type type, [CanBeNull] Dictionary<string, string> keyValues)
159162
{
160163
Guard.NotNull(type, nameof(type));
161164

0 commit comments

Comments
 (0)