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

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

<p>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?</p>

<p>Something like...</p>

<pre><code>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);
</code></pre>

Just do:
```
```
object result = formatter.Deserialize(stream); 
Type t = result.GetType();
```
```