Hi all.
Mình mới tự học C# nhưng gặp phải vấn đề khi lưu mảng dữ liệu vào file.
Mình dùng BinaryFormatter.
Code:
[Serializable()]
class OscChannel : ISerializable
{
public bool Status; // Trang thai kenh tat hay bat
public string IdChannel; // Ten kenh
public string Infor; // Thong tin ve kenh
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 2500)]
public byte[] data; // Du lieu cua kenh;
public OscChannel()
{
Status = false;
IdChannel = "ch1";
Infor = "nothing";
data = new byte[2500];
for (int i = 0; i < 2500; i++) data[i] = 0;
}
public OscChannel(string id, string info)
{
Status = false;
IdChannel = id;
Infor = info;
data = new byte[2500];
for (int i = 0; i < 2500; i++) data[i] = 0;
}
// Deserialize
public OscChannel(SerializationInfo info, StreamingContext ctxt)
{
Status = (bool)info.GetValue("status", typeof(bool));
IdChannel = (String)info.GetValue("idchannel", typeof(string));
Infor = (String)info.GetValue("channelInfor", typeof(string));
// Biết sai ở đây nhưng mình chưa tìm được cách lưu mảng này kiểu gì
// Ai giúp mình với
for (int i = 0; i < 2500; i++)
{
data[i] = (byte)info.GetValue("data",typeof(byte));
}
}
// Get object data
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
//You can use any custom name for your name-value pair. But make sure you
// read the values with the same name.
info.AddValue("status", Status);
info.AddValue("idchannel", IdChannel);
info.AddValue("channelInfor", Infor);
for (int i = 0; i < 2500; i++)
{
info.AddValue("data", data[i]);
}
}
}
[Serializable()]
class Oscillo : ISerializable
{
public string SysInfo;
public OscChannel chanel1;
public OscChannel chanel2;
public OscChannel chanel3;
public OscChannel chanel4;
public Oscillo()
{
SysInfo = "";
chanel1 = new OscChannel("ch1","");
chanel2 = new OscChannel("ch2","");
chanel3 = new OscChannel("ch3","");
chanel4 = new OscChannel("ch4","");
}
// Deserialize
public Oscillo(SerializationInfo info, StreamingContext ctxt)
{
SysInfo = (string)info.GetValue("sysInfo", typeof(string));
chanel1 = (OscChannel)info.GetValue("channel1", typeof(OscChannel));
chanel2 = (OscChannel)info.GetValue("channel2", typeof(OscChannel));
chanel3 = (OscChannel)info.GetValue("channel3", typeof(OscChannel));
chanel4 = (OscChannel)info.GetValue("channel4", typeof(OscChannel));
}
public void GetObjectData(SerializationInfo info, StreamingContext ctxt)
{
info.AddValue("sysInfo", SysInfo);
info.AddValue("channel1", chanel1);
info.AddValue("channel2", chanel2);
info.AddValue("channel3", chanel3);
info.AddValue("channel4", chanel4);
}
}
vấn đề là mình bị lỗi khi lưu tới mảng byte[] data.
for (int i = 0; i < 2500; i++)
{
data[i] = (byte)info.GetValue("data",typeof(byte));
}
cám ơn mọi người quan tâm 