指定的数组不属于需要的类型
public struct CStationTable1Data
{
public char[] StationName;
public char[] StationNumber;
public char[] RadarHeight;
...
public void Init()
{
StationName = new char[60];
StationNumber = new char[10];
RadarHeight = new char[10];
...
}
}
public byte[] rawSerialize(object obj)
{
try
{
int rawsize = Marshal.SizeOf(obj);
byte[] rawdatas = new byte[rawsize];
IntPtr buffer = Marshal.AllocHGlobal(rawsize); //分配结构体大小的内存空间?
Marshal.StructureToPtr(obj, buffer, false); //将结构体拷到分配好的内存空间?
Marshal.Copy(buffer, rawdatas, 0, rawsize); //从内存空间拷到byte数组?
Marshal.FreeHGlobal(buffer); //释放内存空间?
return rawdatas;
}
catch (Exception ex)
{
throw ex;
}
}
//初始化结构体
CStationTable1Data StationTable1Data;
StationTable1Data.Init();
char[] StationName = "aaaa".ToCharArray();
char[] StationNumber = "11123".ToCharArray();
char[] RadarHeight = "11123".ToCharArray();
Array.Copy(StationName, StationTable1Data.StationName, StationName.Length);
Array.Copy(StationNumber, StationTable1Data.StationNumber, StationNumber.Length);
Array.Copy(RadarHeight, StationTable1Data.RadarHeight, RadarHeight.Length);
//输出到文件