Skip to main content
Version: 3.0.8

Humanizer.EnumDehumanizeExtensions

EnumDehumanizeExtensions Class​

Contains extension methods for dehumanizing Enum string values.

public static class EnumDehumanizeExtensions

Inheritance System.Object → EnumDehumanizeExtensions

Methods​

EnumDehumanizeExtensions.DehumanizeTo(this string, Type, OnNoMatch) Method​

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);

Parameters​

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.

Returns​

System.Enum
The enum value (as System.Enum) that matches the input string.

Exceptions​

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.

Example​

enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo(typeof(UserType)) => UserType.AnonymousUser (as Enum)

Remarks​

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.

EnumDehumanizeExtensions.DehumanizeTo<TTargetEnum>(this string) Method​

Converts a humanized string back to its original enum value by matching it against enum member names and their humanized representations (including System.ComponentModel.DescriptionAttribute values).

public static TTargetEnum DehumanizeTo<TTargetEnum>(this string input)
where TTargetEnum : struct, System.Enum;

Type parameters​

TTargetEnum

The enum type to convert to. Must be a struct and implement System.Enum.

Parameters​

input System.String

The humanized string to be converted back to an enum value. Must not be null.

Returns​

TTargetEnum
The enum value that matches the input string.

Exceptions​

System.ArgumentException
Thrown when TTargetEnum is not an enum type.

NoMatchFoundException
Thrown when no enum member matches the input string (including checking member names, humanized names, and description attributes).

Example​

enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo<UserType>() => UserType.AnonymousUser
"Registered user".DehumanizeTo<UserType>() => UserType.RegisteredUser
"AnonymousUser".DehumanizeTo<UserType>() => UserType.AnonymousUser

Remarks​

The method attempts to match the input string against: 1. The exact enum member name 2. The humanized version of the enum member name 3. Any System.ComponentModel.DescriptionAttribute value on the enum member Matching is case-sensitive.

EnumDehumanizeExtensions.DehumanizeTo<TTargetEnum>(this string, OnNoMatch) Method​

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;

Type parameters​

TTargetEnum

The enum type to convert to. Must be a struct and implement System.Enum.

Parameters​

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.

Returns​

System.Nullable<TTargetEnum>
The enum value that matches the input string, or null if no match is found and onNoMatch is set to ReturnsNull.

Exceptions​

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.

Example​

enum UserType { AnonymousUser, RegisteredUser }
"Anonymous user".DehumanizeTo<UserType>() => UserType.AnonymousUser
"Invalid".DehumanizeTo<UserType>(OnNoMatch.ReturnsNull) => null
"Invalid".DehumanizeTo<UserType>(OnNoMatch.ThrowsException) => throws NoMatchFoundException

Remarks​

This overload provides more control over error handling compared to the parameterless version.