How can I get the memory address of a feilds in C#?

created an object from a class in C#. Is it possible to retrieve its memory location and address using C# code?

i have tried this code but i got error at this line
IntPtr address = GCHandle.AddrOfPinnedObject(handle);
of the code
c#
Person person_1 =  new Person("mina remon shaker");
FieldInfo? fieldInfo = typeof(Person).GetField("Name");
GCHandle handle = GCHandle.Alloc(person_1, GCHandleType.Pinned);
IntPtr address = GCHandle.AddrOfPinnedObject(handle);
handle.Free();
Console.WriteLine("Object address: 0x{0:X}", address.ToInt64());

`
Was this page helpful?