473,387 Members | 1,790 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,387 software developers and data experts.

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 1778
"Evan Camilleri" <ev**@holisticrd.com.nospamwrote in message
news:ee****************@TK2MSFTNGP02.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**@holisticrd.com.nospamwrote in message
news:ee****************@TK2MSFTNGP02.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.GetBytes() 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[] Object2ByteArray(object o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, o);
return ms.ToArray();
}
public static object ByteArray2Object(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[] Object2ByteArray(T o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, o);
return ms.ToArray();
}
public static T ByteArray2Object(byte[] b)
{
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
return (T)bf.Deserialize(ms);
}
}

Arne

Aug 31 '07 #5

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

Similar topics

4
by: Jason | last post by:
How can I create a stream object from webservice that returns an image in byte format?
6
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...
11
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:...
6
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...
2
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
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...
0
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...
6
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...
3
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...
1
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...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
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
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,...

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.