views:

1933

answers:

3

I want to deserialize an object but don't know the class up front. So, consider the following code...

IFormatter formatter = new BinaryFormatter();
Stream stream = new FileStream("MyFile.bin", FileMode.Open, FileAccess.Read, FileShare.Read);
MyObject obj = (MyObject)formatter.Deserialize(stream);

What could I do if I don't know the class up front? Say, for example "MyFile.bin" was a MyObject or a MyFoo. How do I determine which object to instantiate?

Something like...

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);
+6  A: 

Just do:

object result = formatter.Deserialize(stream); 
Type t = result.GetType();
leppie
A: 

A few suggestions,

  1. If you deserialize the object without casting object myObject = formatter.Deserialize(stream); and then use the "as" operator to check for type compatibility to known types then that might work.

  2. Take a look at BinaryFormatter.Binder property which is of type SerializationBinder, we've used it before to do backward compatibility for older versions of our file format and it worked out great. Basically allows you to totally control what something gets deserialized as.

Sijin
+1  A: 

Mainly as leppie says...

If you want to test it for a few known types, you can use "is"/"as":

MyFoo foo = result As MyFoo;
if(foo != null) { // it was one of those
  // special code
}

But in general, you would let the serializer worry about such details...

It is very different with xml-based serializers, of course, since you need to tell the serializer what is expected, rather than the serializer telling you what it got.

Marc Gravell