473,657 Members | 2,432 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

Object to Memory Stream


How can I trasform (as fast as possible) an object to a binary memory
stream?

Evan
Aug 31 '07 #1
4 34014
"Evan Camilleri" <ev**@holisticr d.com.nospamwro te in message
news:ee******** ********@TK2MSF TNGP02.phx.gbl. ..
>
How can I trasform (as fast as possible) an object to a binary memory
stream?

Evan
Is the object a structure or of a particular class? If a class instance, is
it your own implementation?

Best Regards,
Stanimir Stoyanov
www.stoyanoff.info | www.aeroxp.org

Aug 31 '07 #2
Hi,
"Evan Camilleri" <ev**@holisticr d.com.nospamwro te in message
news:ee******** ********@TK2MSF TNGP02.phx.gbl. ..
>
How can I trasform (as fast as possible) an object to a binary memory
stream?
You can use Serialization, in case that the object is serializable. In any
case it will consist in a number of calls to BitConverter.Ge tBytes() method.
Aug 31 '07 #3
Use the BinaryFormatter class and its Serialize method.
-- Peter
Recursion: see Recursion
site: http://www.eggheadcafe.com
unBlog: http://petesbloggerama.blogspot.com
BlogMetaFinder: http://www.blogmetafinder.com

"Evan Camilleri" wrote:
>
How can I trasform (as fast as possible) an object to a binary memory
stream?

Evan
Aug 31 '07 #4
Evan Camilleri wrote:
How can I trasform (as fast as possible) an object to a binary memory
stream?
Simple code:

public static byte[] Object2ByteArra y(object o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize(ms , o);
return ms.ToArray();
}
public static object ByteArray2Objec t(byte[] b)
{
MemoryStream ms = new MemoryStream(b) ;
BinaryFormatter bf = new BinaryFormatter ();
ms.Position = 0;
return bf.Deserialize( ms);
}

Of if you like generics:

public class Ser<T>
{
public static byte[] Object2ByteArra y(T o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter ();
bf.Serialize(ms , o);
return ms.ToArray();
}
public static T ByteArray2Objec t(byte[] b)
{
MemoryStream ms = new MemoryStream(b) ;
BinaryFormatter bf = new BinaryFormatter ();
ms.Position = 0;
return (T)bf.Deseriali ze(ms);
}
}

Arne

Aug 31 '07 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
1726
by: Jason | last post by:
How can I create a stream object from webservice that returns an image in byte format?
6
3697
by: Ian Robertson | last post by:
I am trying to write a function that takes a reference to an object as its arguement. The object is created in another function and I am trying to pass the object to another function from within the function that created the object. I have cut out some code as I dont think showing a for loop is helpful. void TMP3_Main_Form::LoadMP3() { TMemoryStream *MP3Stream = new TMemoryStream();...
11
1973
by: jacob navia | last post by:
I am writing software to make a general storage facility of any kind of objects to/from disk. The intermeidate format used is XML, using the schema (modified a bit) of Microsoft: xmlns="x-schema:xop-schema.xml" Operation: ---------- The software generates several C functions that implement the writing of the XML. To make things more concrete suppose
6
41010
by: cameron | last post by:
I need to get the size of an objet in memory. I have tried: System.IO.MemoryStream m = new System.IO.MemoryStream(); System.Runtime.Serialization.Formatters.Binary.BinaryFormatter b = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter(); b.Serialize(m, Obj); double size = Convert.ToDouble(m.Length); but not everything is serializable. The thing is I want to be able to
2
1429
by: lqs | last post by:
hi£¡ i am writing a program using Socket . There is a class , for example: public class Student { public int nAge; public string strFirstName; public string strLastName; }
5
7839
by: Tomaz Koritnik | last post by:
Hi I have many short HTML files stored in a binary stream storage to display descriptions for various items in application. HTML would be display inside application using some .NET control or COM control (like Microsoft WebBrowser). For each description there is one HTML file and along description text, it contains links to related information. Clicking related information would for example open new form in application and display some...
0
1220
by: student | last post by:
I have an object created called objM_message. I want to serialize it as xml and place the xml in my HttpWebRequest.GetRequestStream. But the line resp = req.GetResponse doesnt return any results .. it should do My question is this. Is the WriteTo() method the proper one to use to pass the contents of my memory stream into the
6
14329
by: Scirious | last post by:
People, how can I know how big an object is? I mean, I have an object the collects data from a stream and when it grows to an especific size I need to create a new object to continue collecting the data and send the other one to a thread that records it's content to the disk. How do I do such thing? TIA, Scirious.
3
5549
by: =?Utf-8?B?UGhpbCBKb2huc29u?= | last post by:
Hi, I am using dotnet remoting with a binarry formatter. I have a property that returns a memorystream that has had a file loaded into it. When I try to access this property though I get an error regarding "the proxy has no channel sink.......or no suitable Client channel to talk to the server."
1
8649
Cathode Follower
by: Cathode Follower | last post by:
Cloning in vb6 was always a pain. It was so easy to get the code wrong, and the problems it caused were endless - normally stack overflow. In .Net you do not have to do any cloning any more - you can simply serialise the object and then copy it into the target object. The technique is quite widely used and described - but it is interesting to use it with generics, as it means that you can use it anywhere in your application. Here is a code...
0
8732
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 captivates audiences and drives business growth. The Art of Business Website Design Your website is...
0
8605
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 choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
7330
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
6166
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 instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
4155
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 the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
4306
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
2726
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system
2
1955
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1615
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.