Replies: 1 comment 10 replies
-
Hi, field offsets can change, so you shouldn't rely on them being fixed. If you absolutely need to access the field of an unmanaged reference and the class is unconfigured (i.e. no [FieldOffset] attribute), you can use the System.Runtime.CompilerServices.Unsafe class to get the field offset at runtime. The Unsafe.OffsetOf method returns an IntPtr that represents the byte offset of the specified field in the type, which can be converted to a long value using the ToInt64 method. |
Beta Was this translation helpful? Give feedback.
10 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hi there,
Do field offsets get moved around? I noticed there was a NotImplemented.ByDesign if I recall poking around the source. Since there's no guarantee the internal metadata for a Class will stay the same, how would I get the field offset myself? (by just having access to typeof(ClassName))
Edit: Also note that the class is unstructured, so no [FieldOffset] and such (so I can't use Marshal.OffsetOf)
Beta Was this translation helpful? Give feedback.
All reactions