Reverse Enum Lookup

Sometimes you may have the string name of an enumeration identifier, and want its enum type. For example, you write a enum identifier as a string to a file and want to read it back and obtain the original enumeration type. This takes place when you serialize a class to an XML file and deserialize it back using XmlSerializer.

private enum Direction {North, South, East, West};

private Direction ParseDirection(string name)
{ return (Direction) Enum.Parse(typeof(Direction), name, true); }

[Via Noah Coad]

4 Comments

Comments have been disabled for this content.