C # example: serialized access class.

Source: Internet
Author: User

It may be my bad habits. It may also be that I think the best way for programmers to communicate is code, or that I am too confident that my code is easy to understand, or that the program I write is itself a work of art. So I just want to paste the Code:

 

The class is defined as follows:

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> // a non-parameter constructor is required for deserialization. <br/> Public myclass () <br/>{< br/>}< br/>

 

An example of read/write operations is as follows:

 

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> da TAS = new list <myclass> (); <br/> datas. addrange (New myclass [] {New myclass (1, 1, 1), new myclass (2, 2, 2, 2), new myclass (3, 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 = 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 = 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/> console. writeline ("press any exit program. "); <br/> console. readkey (); <br/>}</P> <p>

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.