繁体   English   中英

尝试从.bin文件读取时,InvalidCastException / Serialization异常

[英]InvalidCastException/Serialization Exception when trying to read from .bin file

我正在为CS:GO开发热图工具。 它将解密.dem(自定义演示)文件,然后将它们序列化为二进制文件以备后用。

被序列化的数据如下所示:

namespace HeatScatterToolv2
{
class LoadAndVisualize
{
    public void LoadFileFromBinary (string fileName)
    {
        MatchInfo matchInfo = BinarySerialization.ReadFromBinaryFile<MatchInfo>(fileName);
        List<PlayerInfo> listOfPlayers = BinarySerialization.ReadFromBinaryFile<List<PlayerInfo>>(fileName);
        List<RoundEndEvent> listOfRoundEndEvents = BinarySerialization.ReadFromBinaryFile<List<RoundEndEvent>>(fileName);
        List<BombPlantEvent> listOfBombPlants = BinarySerialization.ReadFromBinaryFile<List<BombPlantEvent>>(fileName);
        List<BombDefuseEvent> listOfBombDefuses = BinarySerialization.ReadFromBinaryFile<List<BombDefuseEvent>>(fileName);
        List<BombExplodeEvent> listOfBombExplosions = BinarySerialization.ReadFromBinaryFile<List<BombExplodeEvent>>(fileName);
        List<SmokeEvent> listOfSmokes = BinarySerialization.ReadFromBinaryFile<List<SmokeEvent>>(fileName);
        List<MolotovEvent> listOfMolotovs = BinarySerialization.ReadFromBinaryFile<List<MolotovEvent>>(fileName);
        List<FlashEvent> listOfFlashes = BinarySerialization.ReadFromBinaryFile<List<FlashEvent>>(fileName);
        List<GrenadeEvent> listOfGrenades = BinarySerialization.ReadFromBinaryFile<List<GrenadeEvent>>(fileName);
        List<KillEvent> listOfKills = BinarySerialization.ReadFromBinaryFile<List<KillEvent>>(fileName);
    }
}

这些是[Serializable]结构的列表,每个结构都有一些类型为int / float / long / string / char的变量。

但是,当执行此代码时,我收到一个InvalistCastException: InvalidCastException警告

将鼠标悬停在Stream实例上,这向我显示了sme读/写超时。

如果我继续运行程序,则会得到SerializationException:

序列化异常

这是所有这些发生的代码:

public static T ReadFromBinaryFile<T>(string filePath)
    {
        using (Stream stream = File.Open(filePath, FileMode.Open))
        {
            bool canItTimeout = stream.CanTimeout;
            var binaryFormatter = new BinaryFormatter();
            return (T)binaryFormatter.Deserialize(stream);
        }
    }

我是编程新手,还是C#新手。 我阅读了有关类似问题的其他问题,但无法获得他们的任何解决方案,或者我不太了解它们。

由于我没有评级,因此我无法发布2个以上的链接,但以下是与此问题相关的课程(据我所知)。

我的序列化程序类(从其他人复制):https:///./pastebin.com/XHUqgq8y

我的保存二进制文件类:https:///./pastebin.com/xqBw9YCY

我使用的结构:https:/./ pastebin.com/gDUuvAdh

我的加载类如下:https:///./pastebin.com/NktKzFfD

程序类:https:///./pastebin.com/eTxFmHbn

抱歉,我的代码链接了一半,但是我真的不知道可能导致错误的原因。

非常感谢您的帮助,但如有可能,请为像我这样的新手解释。 谢谢!

您有一些设计问题1)首先,您不能继续打开同一文件。 因此,将序列化方法传递给流。2)对于二进制数据,如果列表中的项目不知道编号。 因此,您必须将计数传递给序列化方法。 我假设matchInfo将包含其他列表对象的计数。

       public void LoadFileFromBinary(string fileName)
        {
            using (Stream stream = File.Open(filePath, FileMode.Open))
            {

                MatchInfo matchInfo = BinarySerialization.ReadFromBinaryFile<MatchInfo>(stream);
                int count = 1;
                List<PlayerInfo> listOfPlayers = BinarySerialization.ReadFromBinaryFile<List<PlayerInfo>>(stream,count);
                List<RoundEndEvent> listOfRoundEndEvents = BinarySerialization.ReadFromBinaryFile<List<RoundEndEvent>>(stream, count);
                List<BombPlantEvent> listOfBombPlants = BinarySerialization.ReadFromBinaryFile<List<BombPlantEvent>>(stream, count);
                List<BombDefuseEvent> listOfBombDefuses = BinarySerialization.ReadFromBinaryFile<List<BombDefuseEvent>>(stream, count);
                List<BombExplodeEvent> listOfBombExplosions = BinarySerialization.ReadFromBinaryFile<List<BombExplodeEvent>>(stream, count);
                List<SmokeEvent> listOfSmokes = BinarySerialization.ReadFromBinaryFile<List<SmokeEvent>>(stream, count);
                List<MolotovEvent> listOfMolotovs = BinarySerialization.ReadFromBinaryFile<List<MolotovEvent>>(stream, count);
                List<FlashEvent> listOfFlashes = BinarySerialization.ReadFromBinaryFile<List<FlashEvent>>(stream, count);
                List<GrenadeEvent> listOfGrenades = BinarySerialization.ReadFromBinaryFile<List<GrenadeEvent>>(stream, count);
                List<KillEvent> listOfKills = BinarySerialization.ReadFromBinaryFile<List<KillEvent>>(stream, count);
            }
        }

        public static List<T> ReadFromBinaryFile<T>(Stream stream, int count)
        {
            List<T> data = new List<T>();
            var binaryFormatter = new BinaryFormatter();

            for (int i = 0; i < count; i++)
            {
                data.Add((T)binaryFormatter.Deserialize(stream));
            }

            return data;
        }

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM