• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

loresoft / FluentCommand / 16301005024

15 Jul 2025 06:03PM UTC coverage: 54.951%. First build
16301005024

push

github

pwelter34
import data tweaks, xml doc updates

1716 of 3630 branches covered (47.27%)

Branch coverage included in aggregate %.

78 of 143 new or added lines in 11 files covered. (54.55%)

4361 of 7429 relevant lines covered (58.7%)

231.01 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

25.26
/src/FluentCommand/Extensions/ConvertExtensions.cs
1
using System.Globalization;
2

3
#nullable enable
4

5
namespace FluentCommand.Extensions;
6

7
/// <summary>
8
/// Provides extension methods for safely converting string values to various base data types, supporting nullable types and culture-specific formatting.
9
/// </summary>
10
public static class ConvertExtensions
11
{
12
    /// <summary>
13
    /// Converts the specified string representation of a logical value to its Boolean equivalent.
14
    /// Accepts common true/false representations such as "true", "false", "yes", "no", "1", "0", "on", "off", "y", "n", "t", "f", and is case-insensitive.
15
    /// </summary>
16
    /// <param name="value">A string containing the logical value to convert.</param>
17
    /// <returns>
18
    /// <c>true</c> if <paramref name="value"/> represents a true value; otherwise, <c>false</c>.
19
    /// </returns>
20
    public static bool ToBoolean(this string? value)
21
    {
22
        if (value == null)
4!
23
            return false;
×
24

25
        if (bool.TryParse(value, out var result))
4✔
26
            return result;
3✔
27

28
        string v = value.Trim();
1✔
29

30
        if (string.Equals(v, "t", StringComparison.OrdinalIgnoreCase)
1!
31
            || string.Equals(v, "true", StringComparison.OrdinalIgnoreCase)
1✔
32
            || string.Equals(v, "y", StringComparison.OrdinalIgnoreCase)
1✔
33
            || string.Equals(v, "yes", StringComparison.OrdinalIgnoreCase)
1✔
34
            || string.Equals(v, "1", StringComparison.OrdinalIgnoreCase)
1✔
35
            || string.Equals(v, "x", StringComparison.OrdinalIgnoreCase)
1✔
36
            || string.Equals(v, "on", StringComparison.OrdinalIgnoreCase))
1✔
37
            return true;
×
38

39
        return false;
1✔
40
    }
41

42
    /// <summary>
43
    /// Converts the specified string representation of a number to an equivalent 8-bit unsigned integer.
44
    /// </summary>
45
    /// <param name="value">A string containing the number to convert.</param>
46
    /// <returns>
47
    /// An 8-bit unsigned integer equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
48
    /// </returns>
49
    public static byte? ToByte(this string? value)
50
    {
51
        if (value == null)
×
52
            return null;
×
53

54
        if (byte.TryParse(value, out var result))
×
55
            return result;
×
56

57
        return null;
×
58
    }
59

60
    /// <summary>
61
    /// Converts the specified string representation of a number to an equivalent 8-bit unsigned integer, using the specified culture-specific formatting information.
62
    /// </summary>
63
    /// <param name="value">A string containing the number to convert.</param>
64
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
65
    /// <returns>
66
    /// An 8-bit unsigned integer equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
67
    /// </returns>
68
    public static byte? ToByte(this string? value, IFormatProvider provider)
69
    {
70
        if (value == null)
×
71
            return null;
×
72

73
        if (byte.TryParse(value, NumberStyles.Integer, provider, out var result))
×
74
            return result;
×
75

76
        return null;
×
77
    }
78

79
    /// <summary>
80
    /// Converts the specified string representation of a date and time to an equivalent <see cref="DateTime"/> value.
81
    /// Attempts multiple common date formats.
82
    /// </summary>
83
    /// <param name="value">The string representation of a date and time.</param>
84
    /// <returns>
85
    /// The <see cref="DateTime"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
86
    /// </returns>
87
    public static DateTime? ToDateTime(this string? value)
88
    {
89
        if (value == null)
×
90
            return null;
×
91

92
        if (DateTime.TryParse(value, out var result))
×
93
            return result;
×
94

95
        if (DateTime.TryParseExact(value, "M/d/yyyy hh:mm:ss tt", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
×
96
            return result;
×
97

98
        if (DateTime.TryParseExact(value, "M/d/yyyy", CultureInfo.CurrentCulture, DateTimeStyles.None, out result))
×
99
            return result;
×
100

101
        return null;
×
102
    }
103

104
    /// <summary>
105
    /// Converts the specified string representation of a date and time to an equivalent <see cref="DateTime"/> value, using the specified culture-specific formatting information.
106
    /// Attempts multiple common date formats.
107
    /// </summary>
108
    /// <param name="value">A string containing a date and time to convert.</param>
109
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
110
    /// <returns>
111
    /// The <see cref="DateTime"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
112
    /// </returns>
113
    public static DateTime? ToDateTime(this string? value, IFormatProvider provider)
114
    {
115
        if (value == null)
×
116
            return null;
×
117

118
        if (DateTime.TryParse(value, out var result))
×
119
            return result;
×
120

121
        if (DateTime.TryParseExact(value, "M/d/yyyy hh:mm:ss tt", provider, DateTimeStyles.None, out result))
×
122
            return result;
×
123

124
        if (DateTime.TryParseExact(value, "M/d/yyyy", provider, DateTimeStyles.None, out result))
×
125
            return result;
×
126

127
        return null;
×
128
    }
129

130
    /// <summary>
131
    /// Converts the specified string representation of a number to an equivalent decimal number.
132
    /// </summary>
133
    /// <param name="value">A string containing a number to convert.</param>
134
    /// <returns>
135
    /// A <see cref="decimal"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
136
    /// </returns>
137
    public static decimal? ToDecimal(this string? value)
138
    {
139
        if (value == null)
×
140
            return null;
×
141

142
        if (decimal.TryParse(value, NumberStyles.Currency, CultureInfo.CurrentCulture, out var result))
×
143
            return result;
×
144

145
        return null;
×
146
    }
147

148
    /// <summary>
149
    /// Converts the specified string representation of a number to an equivalent decimal number, using the specified culture-specific formatting information.
150
    /// </summary>
151
    /// <param name="value">A string containing a number to convert.</param>
152
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
153
    /// <returns>
154
    /// A <see cref="decimal"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
155
    /// </returns>
156
    public static decimal? ToDecimal(this string? value, IFormatProvider provider)
157
    {
158
        if (value == null)
×
159
            return null;
×
160

161
        if (decimal.TryParse(value, NumberStyles.Currency, provider, out var result))
×
162
            return result;
×
163

164
        return null;
×
165
    }
166

167
    /// <summary>
168
    /// Converts the specified string representation of a number to an equivalent double-precision floating-point number.
169
    /// </summary>
170
    /// <param name="value">A string containing the number to convert.</param>
171
    /// <returns>
172
    /// A <see cref="double"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
173
    /// </returns>
174
    public static double? ToDouble(this string? value)
175
    {
176
        if (value == null)
×
177
            return null;
×
178

179
        if (double.TryParse(value, out var result))
×
180
            return result;
×
181

182
        return null;
×
183
    }
184

185
    /// <summary>
186
    /// Converts the specified string representation of a number to an equivalent double-precision floating-point number, using the specified culture-specific formatting information.
187
    /// </summary>
188
    /// <param name="value">A string containing the number to convert.</param>
189
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
190
    /// <returns>
191
    /// A <see cref="double"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
192
    /// </returns>
193
    public static double? ToDouble(this string? value, IFormatProvider provider)
194
    {
195
        if (value == null)
×
196
            return null;
×
197

198
        if (double.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var result))
×
199
            return result;
×
200

201
        return null;
×
202
    }
203

204
    /// <summary>
205
    /// Converts the specified string representation of a number to an equivalent 16-bit signed integer.
206
    /// </summary>
207
    /// <param name="value">A string containing the number to convert.</param>
208
    /// <returns>
209
    /// A <see cref="short"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
210
    /// </returns>
211
    public static short? ToInt16(this string? value)
212
    {
213
        if (value == null)
×
214
            return null;
×
215

216
        if (short.TryParse(value, out var result))
×
217
            return result;
×
218

219
        return null;
×
220
    }
221

222
    /// <summary>
223
    /// Converts the specified string representation of a number to an equivalent 16-bit signed integer, using the specified culture-specific formatting information.
224
    /// </summary>
225
    /// <param name="value">A string containing the number to convert.</param>
226
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
227
    /// <returns>
228
    /// A <see cref="short"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
229
    /// </returns>
230
    public static short? ToInt16(this string? value, IFormatProvider provider)
231
    {
232
        if (value == null)
×
233
            return null;
×
234

235
        if (short.TryParse(value, NumberStyles.Integer, provider, out var result))
×
236
            return result;
×
237

238
        return null;
×
239
    }
240

241
    /// <summary>
242
    /// Converts the specified string representation of a number to an equivalent 32-bit signed integer.
243
    /// </summary>
244
    /// <param name="value">A string containing the number to convert.</param>
245
    /// <returns>
246
    /// An <see cref="int"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
247
    /// </returns>
248
    public static int? ToInt32(this string? value)
249
    {
250
        if (value == null)
3!
251
            return null;
×
252

253
        if (int.TryParse(value, out var result))
3✔
254
            return result;
2✔
255

256
        return null;
1✔
257
    }
258

259
    /// <summary>
260
    /// Converts the specified string representation of a number to an equivalent 32-bit signed integer, using the specified culture-specific formatting information.
261
    /// </summary>
262
    /// <param name="value">A string containing the number to convert.</param>
263
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
264
    /// <returns>
265
    /// An <see cref="int"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
266
    /// </returns>
267
    public static int? ToInt32(this string? value, IFormatProvider provider)
268
    {
269
        if (value == null)
×
270
            return null;
×
271

272
        if (int.TryParse(value, NumberStyles.Integer, provider, out var result))
×
273
            return result;
×
274

275
        return null;
×
276
    }
277

278
    /// <summary>
279
    /// Converts the specified string representation of a number to an equivalent 64-bit signed integer.
280
    /// </summary>
281
    /// <param name="value">A string containing a number to convert.</param>
282
    /// <returns>
283
    /// A <see cref="long"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
284
    /// </returns>
285
    public static long? ToInt64(this string? value)
286
    {
287
        if (value == null)
×
288
            return null;
×
289

290
        if (long.TryParse(value, out var result))
×
291
            return result;
×
292

293
        return null;
×
294
    }
295

296
    /// <summary>
297
    /// Converts the specified string representation of a number to an equivalent 64-bit signed integer, using the specified culture-specific formatting information.
298
    /// </summary>
299
    /// <param name="value">A string containing the number to convert.</param>
300
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
301
    /// <returns>
302
    /// A <see cref="long"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
303
    /// </returns>
304
    public static long? ToInt64(this string? value, IFormatProvider provider)
305
    {
306
        if (value == null)
×
307
            return null;
×
308

309
        if (long.TryParse(value, NumberStyles.Integer, provider, out var result))
×
310
            return result;
×
311

312
        return null;
×
313
    }
314

315
    /// <summary>
316
    /// Converts the specified string representation of a number to an equivalent single-precision floating-point number.
317
    /// </summary>
318
    /// <param name="value">A string containing the number to convert.</param>
319
    /// <returns>
320
    /// A <see cref="float"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
321
    /// </returns>
322
    public static float? ToSingle(this string? value)
323
    {
324
        if (value == null)
×
325
            return null;
×
326

327
        if (float.TryParse(value, out var result))
×
328
            return result;
×
329

330
        return null;
×
331
    }
332

333
    /// <summary>
334
    /// Converts the specified string representation of a number to an equivalent single-precision floating-point number, using the specified culture-specific formatting information.
335
    /// </summary>
336
    /// <param name="value">A string containing the number to convert.</param>
337
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
338
    /// <returns>
339
    /// A <see cref="float"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
340
    /// </returns>
341
    public static float? ToSingle(this string? value, IFormatProvider provider)
342
    {
343
        if (value == null)
×
344
            return null;
×
345

346
        if (float.TryParse(value, NumberStyles.Float | NumberStyles.AllowThousands, provider, out var result))
×
347
            return result;
×
348

349
        return null;
×
350
    }
351

352
    /// <summary>
353
    /// Converts the specified string representation of a number to an equivalent 16-bit unsigned integer.
354
    /// </summary>
355
    /// <param name="value">A string containing the number to convert.</param>
356
    /// <returns>
357
    /// A <see cref="ushort"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
358
    /// </returns>
359
    public static ushort? ToUInt16(this string? value)
360
    {
361
        if (value == null)
×
362
            return null;
×
363

364
        if (ushort.TryParse(value, out var result))
×
365
            return result;
×
366

367
        return null;
×
368
    }
369

370
    /// <summary>
371
    /// Converts the specified string representation of a number to an equivalent 16-bit unsigned integer, using the specified culture-specific formatting information.
372
    /// </summary>
373
    /// <param name="value">A string containing the number to convert.</param>
374
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
375
    /// <returns>
376
    /// A <see cref="ushort"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
377
    /// </returns>
378
    public static ushort? ToUInt16(this string? value, IFormatProvider provider)
379
    {
380
        if (value == null)
×
381
            return null;
×
382

383
        if (ushort.TryParse(value, NumberStyles.Integer, provider, out var result))
×
384
            return result;
×
385

386
        return null;
×
387
    }
388

389
    /// <summary>
390
    /// Converts the specified string representation of a number to an equivalent 32-bit unsigned integer.
391
    /// </summary>
392
    /// <param name="value">A string containing the number to convert.</param>
393
    /// <returns>
394
    /// A <see cref="uint"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
395
    /// </returns>
396
    public static uint? ToUInt32(this string? value)
397
    {
398
        if (value == null)
×
399
            return null;
×
400

401
        if (uint.TryParse(value, out var result))
×
402
            return result;
×
403

404
        return null;
×
405
    }
406

407
    /// <summary>
408
    /// Converts the specified string representation of a number to an equivalent 32-bit unsigned integer, using the specified culture-specific formatting information.
409
    /// </summary>
410
    /// <param name="value">A string containing the number to convert.</param>
411
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
412
    /// <returns>
413
    /// A <see cref="uint"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
414
    /// </returns>
415
    public static uint? ToUInt32(this string? value, IFormatProvider provider)
416
    {
417
        if (value == null)
×
418
            return null;
×
419

420
        if (uint.TryParse(value, NumberStyles.Integer, provider, out var result))
×
421
            return result;
×
422

423
        return null;
×
424
    }
425

426
    /// <summary>
427
    /// Converts the specified string representation of a number to an equivalent 64-bit unsigned integer.
428
    /// </summary>
429
    /// <param name="value">A string containing the number to convert.</param>
430
    /// <returns>
431
    /// A <see cref="ulong"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
432
    /// </returns>
433
    public static ulong? ToUInt64(this string? value)
434
    {
435
        if (value == null)
×
436
            return null;
×
437

438
        if (ulong.TryParse(value, out var result))
×
439
            return result;
×
440

441
        return null;
×
442
    }
443

444
    /// <summary>
445
    /// Converts the specified string representation of a number to an equivalent 64-bit unsigned integer, using the specified culture-specific formatting information.
446
    /// </summary>
447
    /// <param name="value">A string containing the number to convert.</param>
448
    /// <param name="provider">An object supplying culture-specific formatting information.</param>
449
    /// <returns>
450
    /// A <see cref="ulong"/> equivalent to <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
451
    /// </returns>
452
    public static ulong? ToUInt64(this string? value, IFormatProvider provider)
453
    {
454
        if (value == null)
×
455
            return null;
×
456

457
        if (ulong.TryParse(value, NumberStyles.Integer, provider, out var result))
×
458
            return result;
×
459

460
        return null;
×
461
    }
462

463
    /// <summary>
464
    /// Converts the specified string to an equivalent <see cref="TimeSpan"/> value.
465
    /// </summary>
466
    /// <param name="value">The string representation of a <see cref="TimeSpan"/>.</param>
467
    /// <returns>
468
    /// The <see cref="TimeSpan"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
469
    /// </returns>
470
    public static TimeSpan? ToTimeSpan(this string? value)
471
    {
472
        if (value == null)
×
473
            return null;
×
474

475
        if (TimeSpan.TryParse(value, out var result))
×
476
            return result;
×
477

478
        return null;
×
479
    }
480

481
    /// <summary>
482
    /// Converts the specified string to an equivalent <see cref="Guid"/> value.
483
    /// </summary>
484
    /// <param name="value">The string representation of a <see cref="Guid"/>.</param>
485
    /// <returns>
486
    /// The <see cref="Guid"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
487
    /// </returns>
488
    public static Guid? ToGuid(this string? value)
489
    {
490
        if (value == null)
5!
491
            return null;
×
492

493
        if (Guid.TryParse(value, out var result))
5!
494
            return result;
5✔
495

496
        return null;
×
497
    }
498

499
#if NET6_0_OR_GREATER
500
    /// <summary>
501
    /// Converts the specified string to an equivalent <see cref="DateOnly"/> value.
502
    /// Attempts to parse as <see cref="DateOnly"/>, <see cref="DateTime"/>, or <see cref="DateTimeOffset"/>.
503
    /// </summary>
504
    /// <param name="value">The string representation of a <see cref="DateOnly"/>.</param>
505
    /// <returns>
506
    /// The <see cref="DateOnly"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
507
    /// </returns>
508
    public static DateOnly? ToDateOnly(this string? value)
509
    {
510
        if (value == null)
3!
511
            return null;
×
512

513
        if (DateOnly.TryParse(value, out var dateOnly))
3✔
514
            return dateOnly;
2✔
515

516
        if (DateTime.TryParse(value, out var dateTime))
1!
517
            return DateOnly.FromDateTime(dateTime);
×
518

519
        if (DateTimeOffset.TryParse(value, out var dateTimeOffset))
1!
520
            return DateOnly.FromDateTime(dateTimeOffset.DateTime);
×
521

522
        return null;
1✔
523
    }
524

525
    /// <summary>
526
    /// Converts the specified string to an equivalent <see cref="TimeOnly"/> value.
527
    /// Attempts to parse as <see cref="TimeOnly"/>, <see cref="TimeSpan"/>, <see cref="DateTime"/>, or <see cref="DateTimeOffset"/>.
528
    /// </summary>
529
    /// <param name="value">The string representation of a <see cref="TimeOnly"/>.</param>
530
    /// <returns>
531
    /// The <see cref="TimeOnly"/> equivalent of <paramref name="value"/>, or <c>null</c> if conversion fails or <paramref name="value"/> is <c>null</c>.
532
    /// </returns>
533
    public static TimeOnly? ToTimeOnly(this string? value)
534
    {
535
        if (value == null)
×
536
            return null;
×
537

538
        if (TimeOnly.TryParse(value, out var timeOnly))
×
539
            return timeOnly;
×
540

541
        if (TimeSpan.TryParse(value, out var timeSpan))
×
542
            return TimeOnly.FromTimeSpan(timeSpan);
×
543

544
        if (DateTime.TryParse(value, out var dateTime))
×
545
            return TimeOnly.FromDateTime(dateTime);
×
546

547
        if (DateTimeOffset.TryParse(value, out var dateTimeOffset))
×
548
            return TimeOnly.FromDateTime(dateTimeOffset.DateTime);
×
549

550
        return null;
×
551
    }
552
#endif
553

554
    /// <summary>
555
    /// Safely converts the specified string input to the given <paramref name="type"/>.
556
    /// Supports all common base types, including nullable types, and returns <c>null</c> for empty or invalid input if the type is nullable.
557
    /// </summary>
558
    /// <param name="type">The target type to convert to.</param>
559
    /// <param name="input">The string input to convert.</param>
560
    /// <returns>
561
    /// The converted value as an object, or <c>null</c> if conversion fails and the type is nullable; otherwise, the default value for the type.
562
    /// </returns>
563
    /// <exception cref="ArgumentNullException">Thrown if <paramref name="type"/> is <c>null</c>.</exception>
564
    public static object? SafeConvert(Type type, string? input)
565
    {
566
        if (type is null)
29!
567
            throw new ArgumentNullException(nameof(type));
×
568

569
        // first try string
570
        if (type == typeof(string))
29✔
571
        {
572
            return input;
10✔
573
        }
574

575
        var isNullable = type.IsNullable();
19✔
576
        if ((input?.IsNullOrEmpty() != false) && isNullable)
19!
577
        {
578
            return null;
4✔
579
        }
580

581
        input = input?.Trim();
15!
582
        var underlyingType = type.GetUnderlyingType();
15✔
583

584
        // convert by type
585
        if (underlyingType == typeof(bool))
15✔
586
        {
587
            return input.ToBoolean();
4✔
588
        }
589
        if (underlyingType == typeof(byte))
11!
590
        {
591
            var value = input.ToByte();
×
NEW
592
            return value ?? (isNullable ? null : 0);
×
593
        }
594
        if (underlyingType == typeof(DateTime))
11!
595
        {
596
            var value = input.ToDateTime();
×
NEW
597
            return value ?? (isNullable ? null : DateTime.MinValue);
×
598
        }
599
        if (underlyingType == typeof(decimal))
11!
600
        {
601
            var value = input.ToDecimal();
×
NEW
602
            return value ?? (isNullable ? null : 0);
×
603
        }
604
        if (underlyingType == typeof(double))
11!
605
        {
606
            var value = input.ToDouble();
×
NEW
607
            return value ?? (isNullable ? null : 0);
×
608
        }
609
        if (underlyingType == typeof(short))
11!
610
        {
611
            var value = input.ToInt16();
×
NEW
612
            return value ?? (isNullable ? null : 0);
×
613
        }
614
        if (underlyingType == typeof(int))
11✔
615
        {
616
            var value = input.ToInt32();
3✔
617
            return value ?? (isNullable ? null : 0);
3!
618
        }
619
        if (underlyingType == typeof(long))
8!
620
        {
621
            var value = input.ToInt64();
×
NEW
622
            return value ?? (isNullable ? null : 0);
×
623
        }
624
        if (underlyingType == typeof(float))
8!
625
        {
626
            var value = input.ToSingle();
×
NEW
627
            return value ?? (isNullable ? null : 0);
×
628
        }
629
        if (underlyingType == typeof(ushort))
8!
630
        {
631
            var value = input.ToUInt16();
×
NEW
632
            return value ?? (isNullable ? null : 0);
×
633
        }
634
        if (underlyingType == typeof(uint))
8!
635
        {
636
            var value = input.ToUInt32();
×
NEW
637
            return value ?? (isNullable ? null : 0);
×
638
        }
639
        if (underlyingType == typeof(ulong))
8!
640
        {
641
            var value = input.ToUInt64();
×
NEW
642
            return value ?? (isNullable ? null : 0);
×
643
        }
644
        if (underlyingType == typeof(TimeSpan))
8!
645
        {
646
            var value = input.ToTimeSpan();
×
NEW
647
            return value ?? (isNullable ? null : TimeSpan.Zero);
×
648
        }
649
        if (underlyingType == typeof(Guid))
8✔
650
        {
651
            var value = input.ToGuid();
5✔
652
            return value ?? (isNullable ? null : Guid.Empty);
5!
653
        }
654
#if NET6_0_OR_GREATER
655
        if (underlyingType == typeof(DateOnly))
3!
656
        {
657
            var value = input.ToDateOnly();
3✔
658
            return value ?? (isNullable ? null : DateOnly.MinValue);
3!
659
        }
660
        if (underlyingType == typeof(TimeOnly))
×
661
        {
662
            var value = input.ToTimeOnly();
×
NEW
663
            return value ?? (isNullable ? null : TimeOnly.MinValue);
×
664
        }
665
#endif
666
        return default;
×
667
    }
668

669
    /// <summary>
670
    /// Converts the specified result object to the given <typeparamref name="TValue"/> type.
671
    /// Supports custom conversion functions and safe conversion from string or other types.
672
    /// </summary>
673
    /// <typeparam name="TValue">The target type to convert to.</typeparam>
674
    /// <param name="result">The result object to convert.</param>
675
    /// <param name="convert">An optional custom conversion function.</param>
676
    /// <returns>
677
    /// The converted value as <typeparamref name="TValue"/>, or the default value if conversion fails.
678
    /// </returns>
679
    public static TValue? ConvertValue<TValue>(object? result, Func<object?, TValue>? convert = null)
680
    {
681
        if (result is null || result == DBNull.Value)
29!
682
            return default;
×
683

684
        if (result is TValue valueType)
29✔
685
            return valueType;
16✔
686

687
        if (convert != null)
13!
688
            return convert(result);
×
689

690
        if (result is string stringValue)
13✔
691
            return (TValue?)SafeConvert(typeof(TValue), stringValue);
5✔
692

693
        try
694
        {
695
            return (TValue)Convert.ChangeType(result, typeof(TValue));
8✔
696
        }
NEW
697
        catch
×
698
        {
NEW
699
            return default;
×
700
        }
701
    }
8✔
702
}
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc