Contains extension methods for dehumanizing Enum string values.
public static class EnumDehumanizeExtensionsInheritance System.Object → EnumDehumanizeExtensions
Converts a humanized string back to its original enum value using runtime type information. This is a non-generic overload that accepts the target enum type as a System.Type parameter.
public static System.Enum DehumanizeTo(this string input, System.Type targetEnum, Humanizer.OnNoMatch onNoMatch=Humanizer.OnNoMatch.ThrowsException);input System.String
The humanized string to be converted back to an enum value. Must not be null.
targetEnum System.Type
The System.Type of the target enum. Must be an enum type.
onNoMatch OnNoMatch
Specifies what to do when no matching enum member is found. Default is ThrowsException.
System.Enum
The enum value (as System.Enum) that matches the input string.
NoMatchFoundException
Thrown when no enum member matches the input string and onNoMatch is set to
ThrowsException.
System.ArgumentException
Thrown when targetEnum is not an enum type.
enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo(typeof(UserType)) => UserType.AnonymousUser (as Enum)This method uses reflection and is less type-safe than the generic overload. Use the generic DehumanizeTo<TTargetEnum>(this string, OnNoMatch) method when the target enum type is known at compile time.
Converts a humanized string back to its original enum value by matching it against enum member names, their humanized representations, and configured metadata aliases.
public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input)
where TTargetEnum : struct, System.Enum;TTargetEnum
The enum type to convert to. Must be a struct and implement System.Enum.
input System.String
The humanized string to be converted back to an enum value. Must not be null.
TTargetEnum
The enum value that matches the input string.
System.ArgumentException
Thrown when TTargetEnum is not an enum type.
NoMatchFoundException
Thrown when no enum member matches the input string.
enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo<UserType>() => UserType.AnonymousUser
"Registered user".DehumanizeTo<UserType>() => UserType.RegisteredUser
"AnonymousUser".DehumanizeTo<UserType>() => UserType.AnonymousUserThe method attempts to match the input string against:
- The exact enum member name.
- The humanized version of the enum member name.
- System.ComponentModel.DataAnnotations.DisplayAttribute.Name, System.ComponentModel.DataAnnotations.DisplayAttribute.Description, and System.ComponentModel.DataAnnotations.DisplayAttribute.ShortName values.
- The configured description attribute value when selected as the member's authored description.
Matching is case-insensitive and does not trim whitespace. If aliases collide, later values in the enum's unsigned numeric order take precedence, while the current humanized representation takes precedence over supplemental aliases.
Converts a humanized string back to its original enum value with configurable behavior when no match is found.
public static System.Nullable<TTargetEnum> DehumanizeTo<TTargetEnum>(this string input, Humanizer.OnNoMatch onNoMatch=Humanizer.OnNoMatch.ThrowsException)
where TTargetEnum : struct, System.Enum;TTargetEnum
The enum type to convert to. Must be a struct and implement System.Enum.
input System.String
The humanized string to be converted back to an enum value. Must not be null.
onNoMatch OnNoMatch
Specifies what to do when no matching enum member is found. Default is ThrowsException.
System.Nullable<TTargetEnum>
The enum value that matches the input string, or null if no match is found and
onNoMatch is set to ReturnsNull.
System.ArgumentException
Thrown when TTargetEnum is not an enum type.
NoMatchFoundException
Thrown when no enum member matches the input string and onNoMatch is set to
ThrowsException.
enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo<UserType>() => UserType.AnonymousUser
"Invalid".DehumanizeTo<UserType>(OnNoMatch.ReturnsNull) => null
"Invalid".DehumanizeTo<UserType>(OnNoMatch.ThrowsException) => throws NoMatchFoundExceptionThis overload provides more control over error handling compared to the parameterless version.