So I've seen this question a few times about getting the physical address of a reference object in C# and the general consensus seems to be that this isn't possible since you can't call GCHandle.Alloc
with GCHandleType.Pinned
which will allow you to get the address of the pinned object.
What I find strange though is that in the Visual Studio debugger I can drag the object from the locals window to the Memory debug window and see the address and memory for a reference object. I can even type in the immediate window &obj
and get the actual address of the object (this address correctly corresponds to the one shown in the Memory window).
However why is it I can't make the same call in my compiled code...ie:
object someObject = new Object();
&obj; // Compiler Error: Cannot take the address of, get the size of, or declare a pointer to a managed type ('object')
How is the Immediate\Memory Window allowed to get the address of managed types and even display the layout of memory but C# can't do any of this? Is there any way to get this actual address of a reference object in C#?