Skip to content

Instantly share code, notes, and snippets.

@aevitas
Last active November 16, 2016 12:56
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save aevitas/d915a1163623e84013d635cf65aac4e1 to your computer and use it in GitHub Desktop.
Save aevitas/d915a1163623e84013d635cf65aac4e1 to your computer and use it in GitHub Desktop.
public override unsafe T Read<T>(IntPtr address, bool isRelative = false)
{
Requires.NotEqual(address, IntPtr.Zero, nameof(address));
// We can bypass the marshal completely, unless the type we're reading has marshalling
// directives such as a [MarshalAs] attribute.
bool requiresMarshal = MarshalCache<T>.TypeRequiresMarshal;
var size = requiresMarshal ? MarshalCache<T>.Size : Unsafe.SizeOf<T>();
var buffer = ReadBytes(address, size, isRelative);
fixed (byte* b = buffer)
{
if (requiresMarshal)
return Marshal.PtrToStructure<T>(new IntPtr(b));
return Unsafe.Read<T>(b);
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment