Skip to content

Instantly share code, notes, and snippets.

@x5lcfd
Created February 13, 2017 07:38
Show Gist options
  • Save x5lcfd/047cbbbbc79d9e907fd656df2aa5d9f3 to your computer and use it in GitHub Desktop.
Save x5lcfd/047cbbbbc79d9e907fd656df2aa5d9f3 to your computer and use it in GitHub Desktop.
StringToEnum
// Mono2.6 doesn't have Enum.TryParse
public static TEnum ToEnum<TEnum>(this string strEnumValue, TEnum defaultValue)
{
if (!Enum.IsDefined(typeof(TEnum), strEnumValue))
return defaultValue;
return (TEnum)Enum.Parse(typeof(TEnum), strEnumValue);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment