Exemple #1
0
        public static void Save(IDictionary <DateTime, SolarTerm> data, string filename)
        {
            int i = 0;

            byte[]            solarTremBytes;
            byte[]            solarTremByte;
            SolarTremStruct[] arr = new SolarTremStruct[data.Count];
            int size = Marshal.SizeOf(typeof(SolarTremStruct));

            solarTremBytes = new byte[size * arr.Length];
            foreach (var solarTrem in data)
            {
                SolarTremStruct solarTremDateTime = new SolarTremStruct();
                solarTremDateTime.dateTime  = solarTrem.Key.ToBinary();
                solarTremDateTime.solarTrem = (int)solarTrem.Value;
                arr[i] = solarTremDateTime;

                solarTremByte = Struct2Byte(arr[i]);
                Array.Copy(solarTremByte, 0, solarTremBytes, i * size, solarTremByte.Length);

                i++;
            }

            WriteInfo(solarTremBytes, filename);
        }
Exemple #2
0
        private static SolarTremStruct Byte2Struct(byte[] arr)
        {
            int    structSize = Marshal.SizeOf(typeof(SolarTremStruct));
            IntPtr ptemp      = Marshal.AllocHGlobal(structSize);

            Marshal.Copy(arr, 0, ptemp, structSize);
            SolarTremStruct rs = (SolarTremStruct)Marshal.PtrToStructure(ptemp, typeof(SolarTremStruct));

            Marshal.FreeHGlobal(ptemp);
            return(rs);
        }
Exemple #3
0
        private static byte[] Struct2Byte(SolarTremStruct s)
        {
            int structSize = Marshal.SizeOf(typeof(SolarTremStruct));

            byte[] buffer    = new byte[structSize];
            IntPtr structPtr = Marshal.AllocHGlobal(structSize); //分配结构体大小的内存空间

            Marshal.StructureToPtr(s, structPtr, false);         //将结构体拷到分配好的内存空间
            Marshal.Copy(structPtr, buffer, 0, structSize);      //从内存空间拷到byte数组
            Marshal.FreeHGlobal(structPtr);                      //释放内存空间
            return(buffer);
        }
Exemple #4
0
        public static IDictionary <DateTime, SolarTerm> Load(string filename)
        {
            IDictionary <DateTime, SolarTerm> res = new Dictionary <DateTime, SolarTerm>();

            byte[] bt   = ReadInfo(filename);
            int    size = Marshal.SizeOf(typeof(SolarTremStruct));
            int    num  = bt.Length / size;

            for (int i = 0; i < num; i++)
            {
                byte[] temp = new byte[size];
                Array.Copy(bt, i * size, temp, 0, size);
                SolarTremStruct solar = new SolarTremStruct();
                solar = Byte2Struct(temp);
                res.Add(DateTime.FromBinary(solar.dateTime), (SolarTerm)solar.solarTrem);
            }
            return(res);
        }