Reverse Enum Lookup

Published Tue, Jun 22 2004 1:46 | coad

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

Filed under:

Comments

# TrackBack said on June 23, 2004 7:01 AM:

# TrackBack said on June 23, 2004 7:17 PM: