Get Memory Address of .NET Object (C#)

后端 未结 5 1999
醉酒成梦
醉酒成梦 2021-01-02 03:05

I am trying to track down a bug in the mono runtime where a variable appears to be allocated to one valid object, and then is reassigned later to a bogus object, specificall

5条回答
  •  一生所求
    2021-01-02 03:50

    You should be able to use the GCHandle construct to accomplish this.

    GCHandle objHandle = GCHandle.Alloc(obj,GCHandleType.WeakTrackResurrection);
    int address = GCHandle.ToIntPtr(objHandle).ToInt32(); 
    

    Where 'obj' is the object whose address you're trying to get.

提交回复
热议问题