I am writing an agnostic viewer for a collection of systems that I'm working with. This viewer will show me the generic structure of my data without needing to know the context of the specific system.
I'm trying to deserialise a memory stream which contains only a type Foo<T>
where Foo<T>
inherits from Foo
. From the agnostic viewers point of view, all the data that I require is in Foo. The <T>
part is irrelevant.
Type T is defined in another assembly(s). Under normal operation, the system obviously has all appropriate contextual assemblies loaded. The problem is that when running the viewer, none of the contextual assemblies are loaded. when I attempt to deserialise the instance of Foo, I obviously get an exception because the referenced assembly is not loaded.
I'm trying to detect whether or not I have all required referenced assemblies loaded and thus know whether to try to deserialise the data, or reconstruct the data that I require from other aspects of the class.
I know I can do this using a very simple exception try/ catch block, but, this is not an exception case. I know this is going to happen hundreds, if not thousands of times when I load my data, and this could cause me a nightmare, as I like to have break on exception turned on. I also subscribe to the school of thought that says "Exception - the hint is in the name" and thus Exceptions should not form part of your primary case code.
--------edit 21/10/2013------------
see here for a full illustrative example, but here are the important bits:
Foo class, defined in common:
[Serializable]
public class Foo
{
public string Agnostic { get; set; }
}
[Serializable]
public class Foo<T> : Foo
{
public string Contextual { get; set; }
}
Contextual saving:
BinaryFormatter bf = new BinaryFormatter();
FileInfo tempFile = TempFileGetter.GetTempFile();
Foo<Bar> fooBar = new Foo<Bar>();
fooBar.Agnostic = "Agnostic";
fooBar.Contextual = "Contextual";
using (var fs = tempFile.OpenWrite())
{
bf.Serialize(fs, fooBar);
fs.Flush();
}
Agnostic loading:
BinaryFormatter bf = new BinaryFormatter();
FileInfo tempFile = TempFileGetter.GetTempFile();
using (var fs = tempFile.OpenRead())
{
Foo foo = (Foo)bf.Deserialize(fs);
Controls.DataContext = foo;
}
I mean, there is nothing rocket science in this code, and, if the "agnostic" viewer loads the context viewer as a reference, then it loads fine, but, I don't want to do this, as we won't always have the the contextual libraries to load.