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);
Just do: