i want to access the bytes of an object in C# okey for example :
Serializing an class in WCF the serializer read the all bytes of class object and Finally SOAP message !
Some Thing Like This You Know A Way To Read object Bytes And Recreate object by its Bytes
//x_obj created and we allocate an address & size of RAM (memory) to it Now its byts structure is in the RAM
X_calss x_obj = new X_class();
//and now we want to read the x_obj bytes in RAM
unsafe byte[] READ(X_class x_obj){
xobj_pointer = &x_obj;//pointer to address of obj in RAM
byte[] xobj_bytes = read_from(xobj_pointer,sizeof(x_obj));//Some Way To Read Byte of x_obj
return xobj_bytes;
}
// and now we want to recreate class by it bytes stream
unsafe X_class Creative(byte[] xobj_bytes){
x_pointer = Memory_allocate(sizeof(X_class));//reserve an address of RAM
write_to(x_pointer,xobj_bytes);//write bytes to RAM
X_class x_obj = (X_class)*(x_pointer);//recreate the class by content of pointer
return x_obj;
}
While agreeing with both Jon Skeet and ValtasarIII, getting access to the raw bytes of a variable is possible (in a horrible kind of way) if your data types are structs (with known layouts), contain only value types and you’re allowing
unsafecode.What follows is certainly not a technique that you’d want to use to cross machine boundaries (nor should it really be used at all).
Given a structure called
TestStructdefined like soThe raw bytes of its contents may be obtained like this
And we can build a new one like this
Usage:
And, for completeness, the code for
PrintObject