Is there a .NET data structure I could use for bidirectional lookup?
Here’s the problem: Serialization. My object contains a field which points to one of 10 predefined static objects. When writing to the file, I write a single character representing which of the 10 objects is being referenced. At this point, I need a lookup datastructure which will allow me to get the character code based on the object being referenced. When deserializing, I need to do the reverse. I can think of a lot of other places where I could use such a data structure.
In the case of only 10 cases that will rarely change, a couple of methods using Switch statements would probably suffice.
If you have control of the static objects, then they could all implement a new interface that returns a “serialization code” character:
Therefore, going in that direction is easy: someObject.SerializationCode. Then you could also have your static objects all use a constructor that registers their SerializationCode with a singleton instance that has a Dictionary.
Deserializing, you just take the character and run it through that dictionary to get the static object back.