Click here to Skip to main content
15,899,825 members
Home / Discussions / C#
   

C#

 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson24-May-02 7:54
James T. Johnson24-May-02 7:54 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo24-May-02 19:39
tmagoo24-May-02 19:39 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson25-May-02 4:17
James T. Johnson25-May-02 4:17 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy25-May-02 4:39
Paddy25-May-02 4:39 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo26-May-02 3:35
tmagoo26-May-02 3:35 
GeneralRe: Sockets/Objects/Serializable Pin
Paddy27-May-02 2:37
Paddy27-May-02 2:37 
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo29-May-02 10:59
tmagoo29-May-02 10:59 
GeneralRe: Sockets/Objects/Serializable Pin
James T. Johnson29-May-02 18:02
James T. Johnson29-May-02 18:02 
The binary serializer includes information so that it can deserialize the data correctly. It includes type information (so that it can instantiate the object then load the data into it).

Eric Gunnerson pointed out a way to convert from a byte array to the object which is extremely easy; I haven't figured out how to get the byte array as easily though.

Here is a test program which shows how it works; compile with the /unsafe compiler switch.
using System;
using System.Runtime.InteropServices;

namespace Test
{
  public struct Data
  {
    public int i;
    public int j;
    public int k;
  }
 
  public class Driver
  {
    public static void Main(string [] args)
    {
      byte [] bytes;
      Data data;
      Data foo;
 
      data.i = 1;
      data.j = 2;
      data.k = 3;
 
      bytes = ToByteArray(data);
 
      foo = FromByteArrayToData(bytes);
 
      System.Console.WriteLine("{0}, {1}, {2}", foo.i, foo.j, foo.k);
    }
 
    private static byte[] ToByteArray(object data)
    {
      // Get size of the object
      int sizeofData = Marshal.SizeOf(data);
 
      // Allocate some system memory
      IntPtr pHData = Marshal.AllocHGlobal(sizeofData);
 
      // Fill that memory with the data in 'data'
      Marshal.StructureToPtr(data, pHData, true);
 
      // Create our byte array with the needed number of bytes
      byte [] bytes = new byte[sizeofData];
 
      // Copy data from the system memory back to our managed array
      Marshal.Copy(pHData, bytes, 0, sizeofData);
 
      // Free our system memory
      Marshal.FreeHGlobal(pHData);
 
      // Finally return the byte array, which was filled with the data
      // from the system memory, which contained memory filled by the
      // StructureToPtr call.
      return bytes;
    }
    
    unsafe private static Data FromByteArrayToData(byte[] bytes)
    {
      fixed(byte* pBytes = bytes)
      {
        return *((Data*) pBytes);
      }
    }
  }	
}


I'd be interested in knowing if there is a "short and sweet" version of the ToByteArray method. It seems like unsafe code could do the conversions, since it has no problems converting a byte array to an object.

James

Simplicity Rules!
GeneralRe: Sockets/Objects/Serializable Pin
tmagoo30-May-02 3:14
tmagoo30-May-02 3:14 
GeneralPlatform Interop - Structures and Arrays Pin
Mikko Puonti24-May-02 0:09
Mikko Puonti24-May-02 0:09 
GeneralXmlDocument with DTD driving me insane! Pin
Daniel Turini23-May-02 10:21
Daniel Turini23-May-02 10:21 
QuestionWindows services???? Pin
Rickard Andersson2023-May-02 4:37
Rickard Andersson2023-May-02 4:37 
AnswerRe: Windows services???? Pin
Nish Nishant23-May-02 17:06
sitebuilderNish Nishant23-May-02 17:06 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 20:48
Rickard Andersson2023-May-02 20:48 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 20:52
sitebuilderNish Nishant23-May-02 20:52 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 21:20
Rickard Andersson2023-May-02 21:20 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 21:20
sitebuilderNish Nishant23-May-02 21:20 
GeneralRe: Windows services???? Pin
Rickard Andersson2023-May-02 22:17
Rickard Andersson2023-May-02 22:17 
GeneralRe: Windows services???? Pin
Nish Nishant23-May-02 22:17
sitebuilderNish Nishant23-May-02 22:17 
GeneralRe: Windows services???? Pin
A.A.29-May-02 17:20
A.A.29-May-02 17:20 
QuestionNotifyIcon and ContextMenu Bug ?? Pin
2sky23-May-02 1:28
2sky23-May-02 1:28 
AnswerRe: NotifyIcon and ContextMenu Bug ?? Pin
Nish Nishant23-May-02 1:34
sitebuilderNish Nishant23-May-02 1:34 
GeneralRe: NotifyIcon and ContextMenu Bug ?? Pin
23-May-02 1:57
suss23-May-02 1:57 
GeneralRe: NotifyIcon and ContextMenu Bug ?? Pin
2sky23-May-02 2:05
2sky23-May-02 2:05 
GeneralComponent staying alive Pin
Oyvind Bratland22-May-02 20:32
Oyvind Bratland22-May-02 20:32 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.