By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
448,709 Members | 1,586 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 448,709 IT Pros & Developers. It's quick & easy.

Byte Array

P: n/a


I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through the
array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up with a
small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if I
try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..

GE
Nov 17 '05 #1
Share this Question
Share on Google+
3 Replies


P: n/a
> I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through the array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up with a small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if I try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..


Are we talking about managed ref types here? If so, you should *never* do
this, you're won't get what you expect, and you're likely to end up
corrupting the managed heap if you manipulate it this way.

Why do you need to do this? If you're trying to call into native code
passing this, then there are appropriate methods for this (P/Invoke,
StructLayout, etc).

If you're trying, for example, to save the contents of the class to disk or
something like that, then using Serialization and the Serialization
formatters (e.g. BinaryFormatter) are the way to go.

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #2

P: n/a

I'm using only managed types.

In a nut shell, I'm trying to send whole classes across the network using
the socket class. The socket class only sends and received using Byte Arrays
(__gc array). I *THINK* i've seen some examples of ppl casting a class into
a byte array, sending it, then casting it back into the class on the other
end. I say I think because I was looking for something else when I came
across it, and it was a fair amount ago.

I have to cast into a byte array. I have no choice. I do want to make sure
i get back what I want though. And If I can avoid having to write a whole
bunch of functions to create a byte array, I would be very happy :)

GE

"Tomas Restrepo (MVP)" wrote:
I want to cast a class into a byte array. I've seen some examples of this
floating around, but they all have simple data members. What happens if I
cast something that has a ArrayList or an Array?? Does it loop through

the
array and cast them all and I end up with a big array (This is what I hope
will happen)? Or does it simple cast the pointer address and I end up

with a
small array??

Related question: How do I make sure everything in the class (including
member variables that are subclasses) are included in this cast?? And if

I
try to cast back from a byte array, will I get back everything as it was??

I haven't started in on this project yet, so I can't say as I've tried any
of this..


Are we talking about managed ref types here? If so, you should *never* do
this, you're won't get what you expect, and you're likely to end up
corrupting the managed heap if you manipulate it this way.

Why do you need to do this? If you're trying to call into native code
passing this, then there are appropriate methods for this (P/Invoke,
StructLayout, etc).

If you're trying, for example, to save the contents of the class to disk or
something like that, then using Serialization and the Serialization
formatters (e.g. BinaryFormatter) are the way to go.

--
Tomas Restrepo
to****@mvps.org

Nov 17 '05 #3

P: n/a
Fireangel,
I'm using only managed types.

In a nut shell, I'm trying to send whole classes across the network using
the socket class. The socket class only sends and received using Byte Arrays (__gc array). I *THINK* i've seen some examples of ppl casting a class into a byte array, sending it, then casting it back into the class on the other
end. I say I think because I was looking for something else when I came
across it, and it was a fair amount ago.
That's common unmanaged C++ practice, but won't really do the trick in
managed code.

I have to cast into a byte array. I have no choice. I do want to make sure i get back what I want though. And If I can avoid having to write a whole
bunch of functions to create a byte array, I would be very happy :)


Serialization is your friend. Basically, all you'd need to do is mark your
managed types as [ Serializable ] and use the BinaryFormatter class to
serialize it into a stream and deserialize them from a string.

There are plenty of examples of this on the net...

That said, since it seems you have control of both client and server, why
invent your own protocol? Just use .NET remoting and be done with it....

--
Tomas Restrepo
to****@mvps.org
Nov 17 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.