This is a simple example that flummaxes teams new to testing exceptions, how to test the serialization constructor. Of course, exceptions should be serializable and marked thusly.
private bool Test_Exception_Serialization(T t) where T : Exception {
try {
using (Stream stream = new MemoryStream()) {
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, t);
stream.Position = 0;
T e = (T)formatter.Deserialize(stream);
return (e is T);
}
} catch {
return false;
}
}
Love generics. Love `em
editor note: additional serialization test code can be found here |