Skip to content

Instantly share code, notes, and snippets.

@danzel
Forked from rje/enumfunc.cs
Last active August 29, 2015 14:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save danzel/ffb71b533083f7c5f728 to your computer and use it in GitHub Desktop.
Save danzel/ffb71b533083f7c5f728 to your computer and use it in GitHub Desktop.
public T ReadEnum<T>(string tok) where T : struct
{
if(typeof(T).IsEnum == false)
{
throw new Exception("Generic type " + typeof(T).Name + " passed to ReadEnum<T> is not an enum type");
}
T toReturn = default(T);
try
{
toReturn = (T)Enum.Parse(typeof(T), tok);
}
catch (Exception e)
{
Debug.LogWarning("String " + tok + " is not a valid type in enum " + typeof(T).Name + ", returning default value");
}
return toReturn;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment