c#範例,序列化存取類。

來源:互聯網
上載者:User

可能是我的習慣不好。也可能是我覺得程式員最好的溝通方式就是代碼,又或是我太自信自己的代碼是很容易看懂的,更或者我認為我寫的程式本身就是藝術品。所以,我只想,貼代碼:

 

類定義如下

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>

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.