I'm sick of using Marshal.Copy
, Marshal.Read*
and Marshal.Write*
so I was wondering if there's a way to force casting of an unmanaged memory pointer (of type IntPtr
).
Something like this:
IntPtr pointer = Marshal.AllocateHGlobal(sizeof(Foo));
Foo bar = (Foo)pointer;
bar.fooBar = someValue;
// call some API
Marshal.FreeHGlobal(pointer);
bar = null; // would be necessary?