Utility class
EnumUtils class
The EnumUtils
class contains a number of helpful methods - mostly for parsing enum values.
.NET it self has similar logic for parsing enum values, but the idea with the methods in this class is that they are more forgiving and easier to use.
ParseEnum method
The generic EnumUtils.ParseEnum
method comes with a few overloads. With one of the overloads, you pass a string value as the first parameter and the enum type as a generic parameter:
HttpStatusCode code = EnumUtils.Parse<HttpStatusCode>("ok");
In comparison to .NET's Enum.Parse
method, the same statement would look like:
HttpStatusCode code = (HttpStatusCode) Enum.Parse(typeof(HttpStatusCode), "OK");
The return type of the method is in this case object
, so you will have to cast the result to HttpStatusCode
. Also notice that the string is now OK
, as the value must match the casing of the enum property. The EnumUtils.ParseEnum
method doesn't care about the case, meaning that it will accept a number of different casings:
HttpStatusCode code1 = EnumUtils.ParseEnum<HttpStatusCode>("not-found"); // kebab case
HttpStatusCode code2 = EnumUtils.ParseEnum<HttpStatusCode>("not_found"); // underscore
HttpStatusCode code3 = EnumUtils.ParseEnum<HttpStatusCode>("not found"); // space
All three lines will correctly be parsed to HttpStatusCode.NotFound
. Even though this method is less prone to errors, there will still be string values that can't be parsed to a enum value, which then will trigger an exception. To avoid this, you can specify a second parameter with a fallback value:
HttpStatusCode code4 = EnumUtils.ParseEnum("nope", HttpStatusCode.NotFound);
As nope
isn't an enum value of HttpStatusCode
, the result will instead be HttpStatusCode.NotFound
. Also notice that the method now knows about the enum type from the second parameter, so specifying the generic parameter is optional.
TryParseEnum method
Similar to how .NET has a Enum.TryParse
method, Skybrud.Essentials also has a EnumUtils.TryParseEnum
method that shares the behaviour of the ParseEnum
described above.
Usage would be something like:
if (EnumUtils.TryParseWnum("not found", out HttpStatusCode status)) {
// Yay
}
Similar to EnumUtils.ParseEnum
, a value like not found
will be properly recognized by the EnumUtils.TryParseEnum
method - but not by .NET's Enum.TryParse
method.