-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathObject.To.UInt32.cs
76 lines (61 loc) · 1.93 KB
/
Object.To.UInt32.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
namespace Ace.CSharp.Extensions;
public static partial class ObjectExtensions
{
public static uint ToUInt32(this object? @this, IFormatProvider? provider)
{
return Convert.ToUInt32(@this, provider);
}
public static uint ToUInt32OrDefault(this object? @this, IFormatProvider? provider, uint @default = default)
{
bool isUInt32 = TryConvertToUInt32(@this, provider, out uint result);
return isUInt32 ? result : @default;
}
public static uint? ToUInt32OrNull(this object? @this, IFormatProvider? provider)
{
if (@this is null)
{
return null;
}
bool isUInt32 = TryConvertToUInt32(@this, provider, out uint result);
return isUInt32 ? result : null;
}
public static bool TryConvertToUInt32(this object? @this, IFormatProvider? provider, out uint result)
{
try
{
result = Convert.ToUInt32(@this, provider);
return true;
}
catch (FormatException)
{
result = default;
return false;
}
catch (InvalidCastException)
{
result = default;
return false;
}
catch (OverflowException)
{
result = default;
return false;
}
}
public static uint ToUInt(this object? @this, IFormatProvider? provider)
{
return ToUInt32(@this, provider);
}
public static uint ToUIntOrDefault(this object? @this, IFormatProvider? provider, uint @default = default)
{
return ToUInt32OrDefault(@this, provider, @default);
}
public static uint? ToUIntOrNull(this object? @this, IFormatProvider? provider)
{
return ToUInt32OrNull(@this, provider);
}
public static bool TryConvertToUInt(this object? @this, IFormatProvider? provider, out uint result)
{
return TryConvertToUInt32(@this, provider, out result);
}
}