473,397 Members | 1,974 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,397 software developers and data experts.

C# Serialising an array of structs

4
I want to serialise an array of structs into a MemoryStream so I can pass the contents in a message.

I have written something that will Serialize and Deserialize in individual object. Is there a way of reading from the MemoryStream back into an array? Can I serialize an array directly without iterating?

My test code:
Expand|Select|Wrap|Line Numbers
  1. using System;
  2. using System.IO;
  3. using System.Runtime.Serialization;
  4. using System.Runtime.Serialization.Formatters.Binary;
  5.  
  6. [Serializable]
  7. public struct SConnection
  8. {
  9.   private int m_handle;
  10.   public int handle {
  11.     get {
  12.       return m_handle;
  13.     }
  14.   }
  15.  
  16.   private string m_name;
  17.   public string name {
  18.     get {
  19.       return m_name;
  20.     }
  21.   }
  22.  
  23.   public SConnection(int handle, string name)
  24.     {
  25.       m_handle = handle;
  26.       m_name = name;
  27.     }
  28.  
  29.   public int Length {
  30.     get {
  31.       return m_name.Length + 11;
  32.     }
  33.   }
  34.  
  35.   public override string ToString()
  36.   {
  37.     return string.Format("0x{0:X8}\t{1}", m_handle, m_name);;
  38.   }
  39. }
  40.  
  41. class Test
  42. {
  43.  
  44.   public static void Main(string[] args)
  45.   {
  46.     SConnection con1 = new SConnection(1234,"foo");
  47.     SConnection con2 = new SConnection(5678,"bar");
  48.  
  49.  
  50.     Console.WriteLine (con1.ToString());
  51.     Console.WriteLine (con2.ToString());
  52.  
  53.     BinaryFormatter bf = new BinaryFormatter();
  54.  
  55.     MemoryStream mem_strm = new MemoryStream();
  56.     Console.WriteLine(
  57.                       "Capacity = {0}, Length = {1}, Position = {2}\n",
  58.                       mem_strm.Capacity.ToString(), 
  59.                       mem_strm.Length.ToString(), 
  60.                       mem_strm.Position.ToString());
  61.  
  62.  
  63.     bf.Serialize(mem_strm, con1);
  64.  
  65.     // Write the stream properties to the console.
  66.     Console.WriteLine(
  67.                       "Capacity = {0}, Length = {1}, Position = {2}\n",
  68.                       mem_strm.Capacity.ToString(), 
  69.                       mem_strm.Length.ToString(), 
  70.                       mem_strm.Position.ToString());
  71.  
  72.     bf.Serialize(mem_strm, con2);
  73.  
  74.     // Write the stream properties to the console.
  75.     Console.WriteLine(
  76.                       "Capacity = {0}, Length = {1}, Position = {2}\n",
  77.                       mem_strm.Capacity.ToString(), 
  78.                       mem_strm.Length.ToString(), 
  79.                       mem_strm.Position.ToString());
  80.  
  81.     mem_strm.Seek(0,0);
  82.     SConnection new_con = new SConnection();
  83.  
  84.     new_con = (SConnection)bf.Deserialize(mem_strm);
  85.     Console.WriteLine (new_con.ToString());
  86.     new_con = (SConnection)bf.Deserialize(mem_strm);
  87.     Console.WriteLine (new_con.ToString());
  88.  
  89.     mem_strm.Seek(0,0);
  90.     SConnection[] new_con_arry = new SConnection[3];
  91.     new_con_arry = (SConnection[])bf.Deserialize(mem_strm);
  92.     foreach(SConnection sc1 in new_con_arry){
  93.       Console.WriteLine ("\t{0}", sc1.ToString());
  94.     }
  95.   }
  96. }
  97.  
thanks

dan
Jan 17 '08 #1
0 1122

Sign in to post your reply or Sign up for a free account.

Similar topics

10
by: Kieran Simkin | last post by:
Hi, I wonder if anyone can help me, I've been headscratching for a few hours over this. Basically, I've defined a struct called cache_object: struct cache_object { char hostname; char ipaddr;...
1
by: mrhicks | last post by:
Hello all, I need some advice/help on a particular problem I am having. I have a basic struct called "indv_rpt_rply" that holds information for a particular device in our system which I will...
5
by: Paminu | last post by:
Why make an array of pointers to structs, when it is possible to just make an array of structs? I have this struct: struct test { int a; int b;
26
by: Brett | last post by:
I have created a structure with five fields. I then create an array of this type of structure and place the structure into an array element. Say index one. I want to assign a value to field3 of...
11
by: Cliff Martin | last post by:
Hi, I am reading a fairly large file a line at a time, doing some processing, and filtering out bits of the line. I am storing the interesting information in a struct and then printing it out....
6
by: =?Utf-8?B?QWxleGFuZGVyZmU=?= | last post by:
Hi, I have a C# program that uses an unmanaged dll that has a function similar to the signature below : void f(out MyStruct arr, out int num); // num = actual array length returned The array...
5
by: dev_15 | last post by:
Hi, I'm going through some code and thought that this allocates an array of structs but its supposed according to comments to allocate an array of pointer to structs. What does it actually do ...
2
by: jonpb | last post by:
Using .NET 3.5, I need to pass an array of structs as parameter to a C++ unmanaged function. The C++ dll stores some data in an unmanaged cache, the function writes the values into the array of...
0
by: mjaaland | last post by:
Hi! I've been working with DLLimports passing structs and various other parameters to unmanaged code. I had problems earlier sending pointer to structs to the unmanaged code, and this forum solved...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.