Description:
The MySqlException class is marked [Serializable], but doesn't serialize/deserialize the custom properties it adds, i.e., Number and SqlState.
All class data should be transferred across a serialization boundary.
How to repeat:
Compile and run the following C# code. It prints:
1042
0
The expected output is:
1042
1042
// generate an exception with MySqlException.Number set
MySqlException exception = null;
using (var connection = new MySqlConnection("server=localhost;port=1"))
{
try
{
connection.Open();
}
catch (MySqlException e)
{
exception = e;
}
}
// serialize and deserialize the exception
MySqlException copy = null;
using (var stream = new MemoryStream())
{
var formatter = new BinaryFormatter();
formatter.Serialize(stream, exception);
stream.Position = 0;
copy = (MySqlException) formatter.Deserialize(stream);
}
// the properties aren't preserved across serialization
Console.WriteLine(exception.Number); // 1042
Console.WriteLine(copy.Number); // 0
Suggested fix:
Initialize property values from the SerializationInfo in the constructor, then implement GetObjectData and save the values of the properties to the provided SerializationInfo.
Description: The MySqlException class is marked [Serializable], but doesn't serialize/deserialize the custom properties it adds, i.e., Number and SqlState. All class data should be transferred across a serialization boundary. How to repeat: Compile and run the following C# code. It prints: 1042 0 The expected output is: 1042 1042 // generate an exception with MySqlException.Number set MySqlException exception = null; using (var connection = new MySqlConnection("server=localhost;port=1")) { try { connection.Open(); } catch (MySqlException e) { exception = e; } } // serialize and deserialize the exception MySqlException copy = null; using (var stream = new MemoryStream()) { var formatter = new BinaryFormatter(); formatter.Serialize(stream, exception); stream.Position = 0; copy = (MySqlException) formatter.Deserialize(stream); } // the properties aren't preserved across serialization Console.WriteLine(exception.Number); // 1042 Console.WriteLine(copy.Number); // 0 Suggested fix: Initialize property values from the SerializationInfo in the constructor, then implement GetObjectData and save the values of the properties to the provided SerializationInfo.