473,854 Members | 1,846 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 34181
"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
1735
by: Jason | last post by:
How can I create a stream object from webservice that returns an image in byte format?
6
3711
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
1985
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
41101
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
1434
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
7897
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
1225
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
14346
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
5568
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
8681
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
9903
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 usage, and What is the difference between ONU and Router. Letís take a closer look ! Part I. Meaning of...
0
9752
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
10692
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
10373
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
7083
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5753
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
5946
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4565
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
4167
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.