我正在尝试将字节数组转换为对象。为了消除任何可能的问题,我创建了一个简单的 windows 窗体,它只是调用破坏我的原始代码的函数,我得到了同样的错误。关于发生了什么的任何想法?
private void button1_Click(object sender, EventArgs e)
{
byte[] myArray = new byte[] {1, 2, 3, 4, 5, 6, 7};
object myObject = ByteArrayToObject(myArray);
if(myObject != null)
{
button1.Text = "good";
}
}
private object ByteArrayToObject(byte[] arrBytes)
{
System.Runtime.Serialization.Formatters.Binary.BinaryFormatter binForm = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();
MemoryStream memStream = new MemoryStream(arrBytes);
memStream.Position = 0;
return binForm.Deserialize(memStream);
}