Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/260.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# LdapException序列化问题-错误代码变为零-.NET错误,还是有意的?_C#_.net_Serialization - Fatal编程技术网

C# LdapException序列化问题-错误代码变为零-.NET错误,还是有意的?

C# LdapException序列化问题-错误代码变为零-.NET错误,还是有意的?,c#,.net,serialization,C#,.net,Serialization,我发现当我序列化和反序列化System.DirectoryServices.Protocols.LdapException类时,ErrorCode属性没有被序列化。我的测试代码如下: using System; using System.DirectoryServices.Protocols; using System.IO; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Bin

我发现当我序列化和反序列化
System.DirectoryServices.Protocols.LdapException类时,
ErrorCode
属性没有被序列化。我的测试代码如下:

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);
        }
    }
}
该程序的输出如下:

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.
我在3.5和4.0.NET版本的VS 2008和VS 2010中使用了上述两种序列化机制,并且都有相同的结果。奇怪的是,某些方面是正常的,比如异常消息本身,但其他部分不是

那么,我是不是不应该序列化异常(为什么它们被标记为
Serializable
属性?),这是一个症状,还是.NET framework中的一个bug