-
-
Notifications
You must be signed in to change notification settings - Fork 82
/
Copy pathEnumHelper.cs
93 lines (89 loc) · 3.25 KB
/
EnumHelper.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
namespace Schema.NET;
using System;
using System.Diagnostics;
using System.Diagnostics.CodeAnalysis;
/// <summary>
/// Helper for parsing strings into Enum values.
/// </summary>
internal static class EnumHelper
{
/// <summary>
/// Converts the string representation of the name or numeric value of one or more
/// enumerated constants to an equivalent enumerated object.
/// </summary>
/// <param name="enumType">The enum type to use for parsing.</param>
/// <param name="value">The string representation of the name or numeric value of one or more enumerated constants.</param>
/// <param name="result">When this method returns true, an object containing an enumeration constant representing the parsed value.</param>
/// <returns><see langword="true"/> if the conversion succeeded; <see langword="false"/> otherwise.</returns>
public static bool TryParse(
Type enumType,
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
string? value,
out object? result)
{
#if NETSTANDARD2_0 || NET472 || NET462
try
{
result = Enum.Parse(enumType, value);
return true;
}
catch
{
result = null;
return false;
}
#else
#pragma warning disable IDE0022 // Use expression body for methods
return Enum.TryParse(enumType, value, out result);
#pragma warning restore IDE0022 // Use expression body for methods
#endif
}
/// <summary>
/// Converts the Schema URI representation of the enum type to an equivalent enumerated object.
/// </summary>
/// <param name="enumType">The enum type to use for parsing.</param>
/// <param name="value">The string representation of the name or numeric value of one or more enumerated constants.</param>
/// <param name="result">When this method returns true, an object containing an enumeration constant representing the parsed value.</param>
/// <returns><see langword="true"/> if the conversion succeeded; <see langword="false"/> otherwise.</returns>
public static bool TryParseEnumFromSchemaUri(
Type enumType,
#if NETCOREAPP3_1_OR_GREATER
[NotNullWhen(true)]
#endif
string? value,
out object? result)
{
string? enumString;
if (value is not null && value.StartsWith(Constants.HttpSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
{
#if NETCOREAPP3_0_OR_GREATER
enumString = value[(Constants.HttpSchemaOrgUrl.Length + 1)..];
#else
enumString = value.Substring(Constants.HttpSchemaOrgUrl.Length + 1);
#endif
}
else if (value is not null && value.StartsWith(Constants.HttpsSchemaOrgUrl, StringComparison.OrdinalIgnoreCase))
{
#if NETCOREAPP3_0_OR_GREATER
enumString = value[(Constants.HttpsSchemaOrgUrl.Length + 1)..];
#else
enumString = value.Substring(Constants.HttpsSchemaOrgUrl.Length + 1);
#endif
}
else
{
enumString = value;
}
if (TryParse(enumType, enumString, out result))
{
return true;
}
else
{
Debug.WriteLine($"Unable to parse enumeration of type {enumType.FullName} with value {enumString}.");
return false;
}
}
}