C#是否能够获取引用类型在内存中的地址

2025-01-03 07:57:10
推荐回答(1个)
回答1:

using System;
using System.Runtime.InteropServices;

[StructLayout(LayoutKind.Sequential)]
class Blittable
{
int x;
}

class Program
{
public static unsafe void Main()
{
int i;
object o = new Blittable();
int* ptr = &i;
IntPtr addr = (IntPtr)ptr;

Console.WriteLine(addr.ToString("x"));

GCHandle h = GCHandle.Alloc(o, GCHandleType.Pinned);
addr = h.AddrOfPinnedObject();
Console.WriteLine(addr.ToString("x"));

h.Free();
}
}