I've found that when I serialize and de-serialize the System.DirectoryServices.Protocols.LdapException class
that the ErrorCode
property does not get serialized. My test code is below:
using System;
using System.DirectoryServices.Protocols;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
namespace TestConsole
{
class Program
{
static void Main(string[] args)
{
LdapException origExcept = new LdapException(81, "Error code of 81 was set originally.");
Console.WriteLine("LdapException error code is: " + origExcept.ErrorCode);
Console.WriteLine("LdapException message code is: " + origExcept.Message);
// Try independant deep clone
LdapException ldapClone = (LdapException)deepCloneSerialize(origExcept);
Console.WriteLine("deep clone error code is: " + ldapClone.ErrorCode);
Console.WriteLine("deep clone message code is: " + ldapClone.Message);
// Try binary deep clone
LdapException binaryClone = (LdapException)binaryCloneSerialize(origExcept);
Console.WriteLine("binary clone error code is: " + binaryClone.ErrorCode);
Console.WriteLine("binary clone message code is: " + binaryClone.Message);
}
private static object deepCloneSerialize(object obj)
{
MemoryStream ms = new MemoryStream();
DataContractSerializer testSer = new DataContractSerializer(obj.GetType());
testSer.WriteObject(ms, obj);
// Now de-serialize and return it
ms.Seek(0, SeekOrigin.Begin);
return testSer.ReadObject(ms);
}
private static object binaryCloneSerialize(object obj)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter myBinFormat = new BinaryFormatter();
myBinFormat.Serialize(ms, obj);
// Now de-serialize and return it
ms.Seek(0, SeekOrigin.Begin);
return myBinFormat.Deserialize(ms);
}
}
}
The output of this program is as follows:
LdapException error code is: 81
LdapException message code is: Error code of 81 was set originally.
deep clone error code is: 0
deep clone message code is: Error code of 81 was set originally.
binary clone error code is: 0
binary clone message code is: Error code of 81 was set originally.
I've tried this in both VS 2008 and VS 2010 under 3.5 and 4.0 .开发者_JS百科NET versions, with both of the serialization mechanisms above, and both have the same results. It's odd that certain aspects come through OK, like the exception message itself, but other parts don't.
So, am I just not supposed to serialize exceptions (why are they marked with the Serializable
attribute then?) and this is a symptom, or is this a bug in the .NET framework?
精彩评论