C# "Enum" Serialization - Deserialization to Static Instance
Posted
by Walt W
on Stack Overflow
See other posts from Stack Overflow
or by Walt W
Published on 2010-03-11T18:43:05Z
Indexed on
2010/03/11
18:44 UTC
Read the original article
Hit count: 581
Suppose you have the following class:
class Test : ISerializable {
public static Test Instance1 = new Test {
Value1 = "Hello"
,Value2 = 86
};
public static Test Instance2 = new Test {
Value1 = "World"
,Value2 = 26
};
public String Value1 { get; private set; }
public int Value2 { get; private set; }
public void GetObjectData(SerializationInfo info, StreamingContext context) {
//Serialize an indicator of which instance we are - Currently
//I am using the FieldInfo for the static reference.
}
}
I was wondering if it is possible / elegant to deserialize to the static instances of the class?
Since the deserialization routines (I'm using BinaryFormatter, though I'd imagine others would be similar) look for a constructor with the same argument list as GetObjectData()
, it seems like this can't be done directly . . Which I would presume means that the most elegant solution would be to actually use an enum
, and then provide some sort of translation mechanism for turning an enum value into an instance reference.
How might one go about this?
© Stack Overflow or respective owner