Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>If you don't need the richness of serialization - if you just wanna write a structure to a byte array, consider the Marshal class. </p> <p>For example, consider a tar app in C#. The tar format is based on 512-byte blocks, and the first block in a series has a regular structure. Ideally the app wants to just <a href="http://en.wikipedia.org/wiki/Blittable_types" rel="nofollow noreferrer">blitt</a> the data from the disk file, <em>right into a structure</em>. The <a href="http://msdn.microsoft.com/en-us/library/4ca6d5z7.aspx" rel="nofollow noreferrer">Marshal.PtrToStructure</a> method does this. Here's the structure. </p> <pre><code> [StructLayout(LayoutKind.Sequential, Size=512)] internal struct HeaderBlock { [MarshalAs(UnmanagedType.ByValArray, SizeConst = 100)] public byte[] name; // name of file. [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] mode; // file mode [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] uid; // owner user ID [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] gid; // owner group ID [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public byte[] size; // length of file in bytes [MarshalAs(UnmanagedType.ByValArray, SizeConst = 12)] public byte[] mtime; // modify time of file [MarshalAs(UnmanagedType.ByValArray, SizeConst = 8)] public byte[] chksum; // checksum for header // ... more like that... up to 512 bytes. </code></pre> <p>Then here's a generic class that does the blitting. </p> <pre><code>internal class RawSerializer&lt;T&gt; { public T RawDeserialize( byte[] rawData ) { return RawDeserialize( rawData , 0 ); } public T RawDeserialize( byte[] rawData , int position ) { int rawsize = Marshal.SizeOf( typeof(T) ); if( rawsize &gt; rawData.Length ) return default(T); IntPtr buffer = Marshal.AllocHGlobal( rawsize ); Marshal.Copy( rawData, position, buffer, rawsize ); T obj = (T) Marshal.PtrToStructure( buffer, typeof(T) ); Marshal.FreeHGlobal( buffer ); return obj; } public byte[] RawSerialize( T item ) { int rawSize = Marshal.SizeOf( typeof(T) ); IntPtr buffer = Marshal.AllocHGlobal( rawSize ); Marshal.StructureToPtr( item, buffer, false ); byte[] rawData = new byte[ rawSize ]; Marshal.Copy( buffer, rawData, 0, rawSize ); Marshal.FreeHGlobal( buffer ); return rawData; } } </code></pre> <p>You can use that class with <em>any</em> structure. You have to use <a href="http://msdn.microsoft.com/en-us/library/system.runtime.interopservices.layoutkind.aspx" rel="nofollow noreferrer">LayoutKind.Sequential</a> and restrict yourself to blittable types (basically primitives and arrays of same) to use this approach. It's fast and efficient, in terms of code and performance and memory, but it's a little restricted in how it can be used. </p> <p>Once you have the byte array, you can transmit it over a NetworkStream or etc, and then de-serialize using the same class on the other end. </p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload