OracleException 没有公共构造函数,也没有任何获取新实例的方法。我尝试了我的 XmlSerializerHelper 类,但它需要一个公共无参数构造函数。
我使用 BinaryFormatter 序列化 OracleException 并将其写入文件。
如何在文件中序列化 OracleException,并使用 XmlSerializer 反序列化 - 出于测试原因 - ?
参考: http: //geekswithblogs.net/WillSmith/archive/2008/07/25/testing-oracleexception.aspx
PD:SoapFormatter 还是 BinaryFormatter 更好?
代码
SerializationHelper.Serialize(@"C:\Temp\ExcepcionOracle.bin", ex);
var exOra = SerializationHelper.Deserialize(@"C:\Temp\ExcepcionOracle.bin");
public static void Serialize(string fileName, Object obj)
{
var binaryFormatter = new BinaryFormatter();
var fileStream = new FileStream(fileName, FileMode.Create);
try
{
binaryFormatter.Serialize(fileStream, obj);
}
catch (SerializationException ex)
{
throw new ApplicationException("The object graph could not be serialized", ex);
}
finally
{
fileStream.Close();
}
}
public static object Deserialize(string fileName)
{
var binaryFormatter = new BinaryFormatter();
var fileStream = new FileStream(fileName, FileMode.Open);
try
{
fileStream.Seek(0, SeekOrigin.Begin);
return binaryFormatter.Deserialize(fileStream);
}
catch (SerializationException ex)
{
throw new ApplicationException("Serialization Exception: " + ex.Message);
}
finally
{
fileStream.Close();
}
return null;
}