I have a binary serialized object in memory and I want to read it from memory by using pointers (unsafae code) in C#. Please look at the following function which is reading from memory stream.
static Results ReadUsingPointers(byte[] data)
{
unsafe
{
fixed (byte* packet = &data[0])
{
return *(Results*)packet;
}
}
}
At this return *(Results*)packet;
statement i get a compile time exception "Cannot take the address of, get the size of, or declare a pointer to a managed type Results"
Here is my structure
public struct Results
{
public int Id;
public int Score;
public char[] Product;
}
As per my understanding, all properties of my struct are blittable properties, then why I am getting this error, and what should I do if I need to use char[] in my structure?
EDIT-1 Let me explain further (plz note that the objects are mocked)...
Background:
I have an array of Results
objects, I serialized them using binary serialization. Now, at later stages of my program, I need to de-serialize my data in memory as quickly as possible as the data volume is very large. So I was trying, how unsafe code can help me there.
Lets say if my structure don't include public char[] Product;
, I get my data back at reasonably good speed. But with char[] it gives me error(compiler should do so). I was looking to find out a solution that work with char[] in this context.