C# 获取变量或对象的栈与堆地址
public class RAM { public void Test() { try { int num_Size = 100000000; var addr = getMemory(num_Size); Console.WriteLine("num_Size addr = " + addr); Person pp = new Person(); pp.Id = 99; pp.Name = "test"; pp.Sex = "nan"; var addr2 = getMemory(pp); Console.WriteLine("num_Size addr = " + addr2); } catch (Exception ex) { } } public string getMemory(object o) // 获取引用类型的内存地址方法 { GCHandle h = GCHandle.Alloc(o, GCHandleType.WeakTrackResurrection); IntPtr addr = GCHandle.ToIntPtr(h); return "0x" + addr.ToString("X"); } } [StructLayout(LayoutKind.Sequential)] public class Person { public int Id { get; set; } public string Name { get; set; } public string Sex { get; set; } }
函数调用的基本原理:http://www.cnblogs.com/swiftma/p/5468104.html