我想反序列化一个对象,但不知道前面的类。所以,考虑下面的代码......
IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);
如果我不知道前面的课程,我该怎么办?比如说,例如“MyFile.bin”是一个 MyObject 或一个 MyFoo。如何确定要实例化哪个对象?
就像是...
if (magic happens here == typeof(MyObject))
MyObject obj = (MyObject) formatter.Deserialize(stream);
else if (more magic happens here == typeof(MyFoo))
MyFoo foo = (MyFoo)formatter.Deserialize(stream);