home | downloads | recipes | blogs | staff | about us | *

The Art of Lightning
Testing exception serializersStuart Williams10/31/2007

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 

Look for more blogs |

(c) 2001-2009 Stuart Williams, E-Mail Stuart Williams