gpt4 book ai didi

C#:输入流不是有效的二进制格式

转载 作者:太空狗 更新时间:2023-10-29 23:10:55 25 4
gpt4 key购买 nike

我在 C#/ASP.NET 中反序列化时遇到问题,它给出了确切的错误:

输入流不是有效的二进制格式。起始内容(以字节为单位)为:41-41-45-41-41-41-44-2F-2F-2F-2F-2F-41-51-41-41-41 .../p>

我想做什么

我有一个包含 3 个类的结构。我有一个作为基类的 A 类,然后是从 A 派生的 B 类和 C 类。

我正在尝试使用 LINQ to SQL 将随机类型的 B 和 C 存储在数据库中类型为 VARCHAR(MAX) 的列中。我不能使用 BINARY,因为长度约为 15.000。

我的代码...

错误在最后一个代码块中

业务层的C#代码-存储记录

    private void AddTraceToDatabase(FightTrace trace)
{
MemoryStream recieverStream = new MemoryStream();
MemoryStream firedStream = new MemoryStream();
MemoryStream moveStream = new MemoryStream();

BinaryFormatter binaryFormatter = new BinaryFormatter();
binaryFormatter.Serialize(recieverStream,trace.Reciever);
binaryFormatter.Serialize(firedStream,trace.FiredBy);
binaryFormatter.Serialize(moveStream,trace.Move);

string reciever = Convert.ToBase64String(recieverStream.ToArray());
string fired = Convert.ToBase64String(firedStream.ToArray());
string move = Convert.ToBase64String(moveStream.ToArray());


this.dataAccess.AddFightTrace(trace.TraceType.ToString(),reciever,move,fired,trace.DateTime,this.FightId);
}

数据访问层中的 C# 代码 - 存储记录

    public void AddFightTrace(string type, string reciever, string Move, string firedBy, DateTime firedAt, int fightid)
{
GameDataContext db = new GameDataContext();
dbFightTrace trace = new dbFightTrace();
trace.TraceType = type;

trace.Reciever = reciever;
trace.Move = Move;
trace.FiredBy = firedBy;
trace.FiredAt = firedAt;
trace.FightId = fightid;

db.dbFightTraces.InsertOnSubmit(trace);
db.SubmitChanges();
}

C# 代码获取数据库中的条目

    public List<dbFightTrace> GetNewTraces(int fightid, DateTime lastUpdate)
{
GameDataContext db = new GameDataContext();
var data = from d in db.dbFightTraces
where d.FightId==fightid && d.FiredAt > lastUpdate
select d;

return data.ToList();
}

C# Factory,将 LINQ to SQL 类转换为我的对象

错误就在这里

    public FightTrace CreateTrace(dbFightTrace trace)
{
TraceType traceType = (TraceType) Enum.Parse(typeof(TraceType), trace.TraceType);
BinaryFormatter formatter = new BinaryFormatter();

System.Text.UTF8Encoding enc = new System.Text.UTF8Encoding();

MemoryStream recieverStream = new MemoryStream(enc.GetBytes(trace.Reciever));
recieverStream.Position = 0;
MemoryStream firedStream = new MemoryStream(enc.GetBytes(trace.FiredBy));
firedStream.Position = 0;
MemoryStream movedStream = new MemoryStream(enc.GetBytes(trace.Move));
movedStream.Position = 0;

// THE NEXT LINE HERE CAUSES THE ERROR
NPC reciever = formatter.Deserialize(recieverStream) as NPC;
Player fired = formatter.Deserialize(firedStream) as Player;
BaseAttack attack = formatter.Deserialize(movedStream) as BaseAttack;

FightTrace t = new FightTrace(traceType,reciever,attack,fired);
t.TraceId = trace.FightTraceId;
t.DateTime = trace.FiredAt;
return t;
}

所以当运行第一个 Deserialize 方法时发生错误,并出现上述错误。

我已经尝试了几件事,但我对这件事很迷茫..

谢谢! :-)

最佳答案

我认为从 base64 转换回字节是错误的。

试试这个:

var bytes = Convert.FromBase64String(base64);

=>

 MemoryStream recieverStream = new MemoryStream(Convert.FromBase64String(trace.Reciever));

NPC reciever = formatter.Deserialize(recieverStream) as NPC;

关于C#:输入流不是有效的二进制格式,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4887902/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com