可能是我的習慣不好。也可能是我覺得程式員最好的溝通方式就是代碼,又或是我太自信自己的代碼是很容易看懂的,更或者我認為我寫的程式本身就是藝術品。所以,我只想,貼代碼:
類定義如下
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using System.Runtime.InteropServices;</p><p>namespace DllTest<br />{<br /> [StructLayout(LayoutKind.Explicit)]<br /> public class MyClass<br /> {<br /> [FieldOffset(0)]<br /> private double x = 0;<br /> [FieldOffset(8)]<br /> private double y = 0;<br /> [FieldOffset(16)]<br /> private double z = 0;<br /> [FieldOffset(24)]<br /> private int id = 0;</p><p> public double X<br /> {<br /> get { return x; }<br /> set { x = value; }<br /> }</p><p> public double Y<br /> {<br /> get { return y; }<br /> set { y = value; }<br /> }</p><p> public double Z<br /> {<br /> get { return z; }<br /> set { z = value; }<br /> }</p><p> public int ID<br /> {<br /> get { return id; }<br /> set { id = value; }<br /> }</p><p> public MyClass(double dx, double dy, double dz, int nid)<br /> {<br /> x = dx;<br /> y = dy;<br /> z = dz;<br /> id = nid;<br /> }</p><p> //還原序列化的時候需要用一個無參建構函式<br /> public MyClass()<br /> {<br /> }<br /> }<br />}<br />
讀寫的例子如下:
using System;<br />using System.Collections.Generic;<br />using System.Linq;<br />using System.Text;<br />using System.Runtime.InteropServices;<br />using System.IO;<br />using DllTest;</p><p>namespace SerializeTest<br />{<br /> class Program<br /> {<br /> static void Main(string[] args)<br /> {</p><p> Console.ReadKey();<br /> }</p><p> public static void WriteSample()<br /> {<br /> List<MyClass> datas = new List<MyClass>();<br /> datas.AddRange(new MyClass[] { new MyClass(1, 1, 1, 1), new MyClass(2, 2, 2, 2), new MyClass(3, 3, 3, 3) });<br /> using (FileStream fs = File.OpenWrite(@"c:/position.bin"))<br /> {<br /> int size = Marshal.SizeOf(typeof(MyClass));<br /> byte[] buffer = new byte[size];<br /> IntPtr ptr = Marshal.AllocHGlobal(size);<br /> foreach (MyClass item in datas)<br /> {<br /> Marshal.StructureToPtr(item, ptr, true);<br /> Marshal.Copy(ptr, buffer, 0, size);<br /> fs.Write(buffer, 0, size);<br /> }<br /> Marshal.FreeHGlobal(ptr);<br /> }<br /> Console.WriteLine("Save complete./r/nPress any key to end program.");<br /> }</p><p> public static void ReadSample()<br /> {<br /> List<MyClass> datas = new List<MyClass>();<br /> int size = Marshal.SizeOf(typeof(MyClass));<br /> using (FileStream fs = File.OpenRead(@"c:/position.bin"))<br /> {<br /> if (fs.Length % size != 0)<br /> {<br /> Console.WriteLine("File size not valid. Maybe error when save file.");<br /> }<br /> else<br /> {<br /> IntPtr ptr = Marshal.AllocHGlobal(size);<br /> byte[] buffer = new byte[size];<br /> while (fs.Read(buffer, 0, size) == size)<br /> {<br /> Marshal.Copy(buffer, 0, ptr, size);<br /> MyClass obj = Marshal.PtrToStructure(ptr, typeof(MyClass)) as MyClass;<br /> if (obj == null)<br /> {<br /> Console.WriteLine("File load faild.");<br /> break;<br /> }<br /> datas.Add(obj);<br /> }</p><p> //read complete<br /> datas.ForEach(m =><br /> {<br /> Console.WriteLine("id={0} x={1} y={2} z={3}", m.ID, m.X, m.Y, m.Z);<br /> });<br /> }<br /> }<br /> Console.WriteLine("Press any exit program.");<br /> Console.ReadKey();<br /> }<br /> }<br />}</p><p>