I have code designed to get a structure from an array of bytes:
public static T GetValue<T>(byte[] data, int start) where T : struct
{
T d = default(T);
int elementsize = Marshal.SizeOf(typeof(T));
GCHandle sh = GCHandle.Alloc(d, GCHandleType.Pinned);
Marshal.Copy(data, start, sh.AddrOfPinnedObject(), elementsize);
sh.Free();
return d;
}
However, the structure dnever changes and always returns the default value.
I was looking for the “right” way to do this, and use it instead, but I'm still interested because I don’t understand why this should not work.
Its as simple as it can be: allocate some memory, d, get a pointer to it, copy some bytes to the memory indicated by this, return. Not only that, but when I use similar code, but with d - an array from T, it works fine. If sh.AddrOfPinnedObject () doesn't really point to d, but then what is its point?
- , ?