vendredi 4 octobre 2019

C# FieldInfo.GetValue on pointer type not matching debugger reported value

Essentially I'm trying to get an internal field via reflection of an internal Unity "DynamicBuffer" class. This field is of type *BufferHeader.

FieldInfo works, returns a System.Reflection.Pointer type, but the value of this pointer is completely different than the one inside the DynamicBuffer, at least as reported by my debugger (jetbrains Rider) as you can see in the image below.

This is not just the debugger being confused, as dereferencing the pointer gives back a struct with garbage values leading me to believe the pointer returned by field info really is wrong for some reason.

(I need more reputation to post images, so just giving a link.)

(https://i.imgur.com/za2ASgb.png)

public unsafe static void testfieldInfo(DynamicBuffer<T> buffer)
        {
            Setup();
            var testFieldInfo = buffer.GetType().GetField(
                "m_Buffer",
                BindingFlags.NonPublic | BindingFlags.Instance);
            var test = testFieldInfo.GetValue(buffer);
            Pointer rawPtr = (Pointer) testFieldInfo.GetValue(buffer);
            BufferHeader* header = (BufferHeader*)Pointer.Unbox(rawPtr);
            BufferHeader headCopy = *header;
            Debug.Log($"pointer Val {new IntPtr(header->Pointer)} original: {new IntPtr((byte*)buffer.GetUnsafePtr())}");
        }




Aucun commentaire:

Enregistrer un commentaire