C#BinaryFormatter。反序列化失败,出现异常

C#BinaryFormatter。反序列化失败,出现异常,c#,serialization,deserialization,C#,Serialization,Deserialization,我在中尝试了示例代码 ,当我在Visual Studio中调试时,结果与预期一致。当我直接在调试文件夹中执行exe文件时,它将得到“解析完成前遇到的流结束”异常 只有当我将项目目标框架设置为“.NET framework 4.8”或4.7版本时,才会发生这种情况。如下图所示: 错误消息: 更奇怪的是,只有在使用.net 4.7和4.8版本时才会发生这种情况。如果我改为.NET4.0版本,这个问题就会消失,甚至会切换回.NET4.7和4.8。这将不再发生 在更高的.net版本中,似乎缺少“Bin

我在中尝试了示例代码 ,当我在Visual Studio中调试时,结果与预期一致。当我直接在调试文件夹中执行exe文件时,它将得到“解析完成前遇到的流结束”异常

只有当我将项目目标框架设置为“.NET framework 4.8”或4.7版本时,才会发生这种情况。如下图所示: 错误消息:

更奇怪的是,只有在使用.net 4.7和4.8版本时才会发生这种情况。如果我改为.NET4.0版本,这个问题就会消失,甚至会切换回.NET4.7和4.8。这将不再发生

在更高的.net版本中,似乎缺少“BinaryFormatter.Deserialize”的某些依赖项?net框架的bug?有人知道如何解决这个问题吗

还要为此问题录制视频:

代码如下所示:

using System;
using System.IO;
using System.Collections;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization;

public class App 
{
[STAThread]
static void Main() 
{
    Serialize();
    Deserialize();

    Console.WriteLine("Press any key to exit.");
    Console.ReadKey();
}

static void Serialize() 
{
    // Create a hashtable of values that will eventually be serialized.
    Hashtable addresses = new Hashtable();
    addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
    addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
    addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");

    // To serialize the hashtable and its key/value pairs,  
    // you must first open a stream for writing. 
    // In this case, use a file stream.
    FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

    // Construct a BinaryFormatter and use it to serialize the data to the stream.
    BinaryFormatter formatter = new BinaryFormatter();
    try 
    {
        formatter.Serialize(fs, addresses);
    }
    catch (SerializationException e) 
    {
        Console.WriteLine("Failed to serialize. Reason: " + e.Message);
        throw;
    }
    finally 
    {
        fs.Close();
    }
}


static void Deserialize() 
{
    // Declare the hashtable reference.
    Hashtable addresses  = null;

    // Open the file containing the data that you want to deserialize.
    FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
    try 
    {
        BinaryFormatter formatter = new BinaryFormatter();

        // Deserialize the hashtable from the file and 
        // assign the reference to the local variable.
        addresses = (Hashtable) formatter.Deserialize(fs);
    }
    catch (SerializationException e) 
    {
        Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
        throw;
    }
    finally 
    {
        fs.Close();
    }

    // To prove that the table deserialized correctly, 
    // display the key/value pairs.
    foreach (DictionaryEntry de in addresses) 
    {
        Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
    }
}
}

谢谢,

这是我作为控制台应用程序运行的代码。唯一的区别是我添加了一个console.readkey,以便查看输出。这是vs2019中的4.8,用于配置的AnyCpu。正如我所说,它在调试模式下运行,并从文件夹中独立运行

using System;
using System.Collections;
using System.IO;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;

namespace ConsoleApp1
{
 class Program
 {
    [STAThread]
    static void Main()
    {
        Serialize();
        Deserialize();

        Console.ReadKey();
    }

    static void Serialize()
    {
        // Create a hashtable of values that will eventually be serialized.
        Hashtable addresses = new Hashtable();
        addresses.Add("Jeff", "123 Main Street, Redmond, WA 98052");
        addresses.Add("Fred", "987 Pine Road, Phila., PA 19116");
        addresses.Add("Mary", "PO Box 112233, Palo Alto, CA 94301");

        // To serialize the hashtable and its key/value pairs,  
        // you must first open a stream for writing. 
        // In this case, use a file stream.
        FileStream fs = new FileStream("DataFile.dat", FileMode.Create);

        // Construct a BinaryFormatter and use it to serialize the data to the stream.
        BinaryFormatter formatter = new BinaryFormatter();
        try
        {
            formatter.Serialize(fs, addresses);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to serialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }
    }


    static void Deserialize()
    {
        // Declare the hashtable reference.
        Hashtable addresses = null;

        // Open the file containing the data that you want to deserialize.
        FileStream fs = new FileStream("DataFile.dat", FileMode.Open);
        try
        {
            BinaryFormatter formatter = new BinaryFormatter();

            // Deserialize the hashtable from the file and 
            // assign the reference to the local variable.
            addresses = (Hashtable)formatter.Deserialize(fs);
        }
        catch (SerializationException e)
        {
            Console.WriteLine("Failed to deserialize. Reason: " + e.Message);
            throw;
        }
        finally
        {
            fs.Close();
        }

        // To prove that the table deserialized correctly, 
        // display the key/value pairs.
        foreach (DictionaryEntry de in addresses)
        {
            Console.WriteLine("{0} lives at {1}.", de.Key, de.Value);
        }
    }
 }
}

你想反序列化什么?这个对象是使用.Net 4序列化的吗?在这里,只需稍加修改就可以了,但从根本上说,我的建议很简单:不要使用
BinaryFormatter
。曾经不幸的是,我看到了这一点,但是:以大多数人试图使用它的方式使用它确实不是一个好主意。几乎任何其他的序列化程序都是一个更好的主意,它将为您节省大量的时间和压力。感谢你们帮助研究这个问题。代码只是中的示例代码,我刚刚运行了您引用的示例代码,在调试或单机版中都没有问题。你确定你没有错过密码吗?我正在运行VS2019 4.8。。。等等,我刚刚为这个问题录制了一段视频,还附上了问题中的代码。谢谢,我也这么做了,我使用了控制台应用程序,控制台应用程序(.net framework)似乎得到了不同的结果……我怀疑我的环境与问题有关,但问题可能是什么?因为一旦我用.NETFramework4.0构建了这个项目,这个问题就不会再发生了,即使我切换到以前会引起麻烦的其他.NETFramework。跟GAC有关?可能是。。。您可以尝试进行修复,看看是否可以修复您的问题,或者卸载,然后重新安装。。。。祝你好运