I'm developing a viewer that will be able to open all of the custom documents that we produce via our software. All of the documents inherit from IDocument, but I'm not sure how to go about deserializing (in a good way - nested try/catch could probably work, but that would be hideous).
So my method as it is now looks like this:
public Boolean OpenDocument(String filename, Type docType, out IDocument document)
{
// exception handling etc. removed for brevity
FileStream fs = null;
BinaryFormatter bFormatter = new BinaryFormatter();
fs = new FileStream(filename, FileMode.Open);
document = (docType)bFormatter.Deserialize(fs);
return true;
}
Obviously this doesn't work as I can't use the variable docType that way, but I think it illustrates the point of what I'm trying to do. What would be the proper way to go about that?
edit> @John ok, maybe I should append another question: if I have an interface:
public interface IDocument
{
public Int32 MyInt { get; }
}
and a class:
public class SomeDocType : IDocument
{
protected Int32 myInt = 0;
public Int32 MyInt { get { return myint; } }
public Int32 DerivedOnlyInt;
}
if I deserialize to an IDocument, will the DerivedOnlyInt be a part of the object - such that after deserializing, I can cast to SomeDocType and it'll be fine?