convert string to enum by type c"# code example
Example 1: string to enum c#
Enum.TryParse("Active", out StatusEnum myStatus);
Example 2: C#: casting string to enum object
public static T ToEnum<T>(this string value, T defaultValue)
{
if (string.IsNullOrEmpty(value))
{
return defaultValue;
}
T result;
return Enum.TryParse<T>(value, true, out result) ? result : defaultValue;
}