views:

164

answers:

4

Is there a way to cast a System.Object to byte*?

A: 

According to Jon Skeet, it is possible through just casting it to a byte array.

monksy
I need cast to unsafe pointer. I ve made a workaround. Passing int through function. I can easily convert byte* to int and int to byte*.But still the question is here.
Michal Franc
@steven i postet jon skets code above ;-)
streetparade
After I posted the source.
monksy
+4  A: 

Just use:

byte[] b = (byte[]) myobj;
streetparade
+2  A: 

How about something like...

BinaryFormatter bf = new BinaryFormatter();
System.IO.MemoryStream ms = new System.IO.MemoryStream(1024);

Object1 blah = new Object1("Hello");

bf.Serialize(ms, blah);
byte[] bytes = ms.GetBuffer();
Germ
A: 

OK, I found the solution:

(byte*)(int)someObject
Michal Franc
What if someObject is larger than an int?
Jan
In my scenario it wont happen. But yes you are right.
Michal Franc
Doesn't work in 64-bit mode. A pointer is larger than an int.
Hans Passant