473,549 Members | 2,583 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

C# Serialising an array of structs

4 New Member
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 1137

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

Similar topics

10
4092
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
1772
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 call INDV. The struct looks like // Some info used for the struct typedef unsigned char uint8; /* 8 bits */
5
3114
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
7050
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 the structure inside the array. When I try this, an error about late assignment appears. Is it possible to assign a value to a structure field...
11
2607
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. This works without any problems. I now would like to filter "duplicate" records. They aren't really duplicate, I can't just qsort as is and...
6
8955
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 must be allocated (with known max length = 10) before the call to the dll function (the dll just fills it ,with no allocations). The definitions of...
5
2279
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 ptrLogArray = new structDisplayLogData *; // iCount = 50 structDisplayLogData is a strcuture of data Thanks
2
4351
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 structs. The array of structs are allocated by C#. If I pass the array without 'ref' it works fine on the C++ side, but after returning to C# the...
0
1649
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 it for me (using the ref keyword etc). I now encountered a function that takes a pointer to an array as a parameter, and this array consists of...
0
7527
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, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main...
0
7726
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, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. ...
0
7967
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that...
1
7485
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For...
0
7819
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the...
1
5377
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 presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes...
0
3505
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in...
0
3488
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1064
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.

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.