這是錯誤即時得到..指定的轉換無效C#控制檯應用程序
http://i.imgur.com/VWTa4Mf.png
這裏是我的代碼..
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Collections;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
namespace LoginServer
{
[Serializable]
class MessageData
{
public int type = 0;
public string stringData = "";
public static MessageData FromByteArray(byte[] input)
{
MemoryStream stream = new MemoryStream(input);
BinaryFormatter formatter = new BinaryFormatter();
MessageData data = new MessageData();
data.type = (int)formatter.Deserialize(stream);
data.stringData = (string)formatter.Deserialize(stream);
if (data.stringData == "")
{
data.type = 999;
data.stringData = "No command included";
}
return data;
}
public static byte[] ToByteArray(MessageData msg)
{
MemoryStream stream = new MemoryStream();
BinaryFormatter formatter = new BinaryFormatter();
formatter.Serialize(stream, msg.type);
formatter.Serialize(stream, msg.stringData);
return stream.ToArray();
}
}
}
真的不知道爲什麼我不能用這個投作爲一個int ..
我花了很多時間試圖解決這個問題,即使使用一個const int但沒有解決問題。
集類型爲int和數據是MessageData,所以INT應該做工精細....
請幫助。因爲你要反序列化應該是MessageData
類型的對象
data = (MessageData)formatter.Deserialize(stream);
:用下面的
data.type = (int)formatter.Deserialize(stream);
data.stringData = (string)formatter.Deserialize(stream);
:
請將錯誤作爲*文本*置於您的問題中並提供[mcve]。 –