c# - Obtain non-explicit field offset -


i have following class:

[structlayout(layoutkind.sequential)] class class {     public int field1;     public byte field2;     public short? field3;     public bool field4; } 

how can byte offset of field4 starting start of class data (or object header)?
illustrate:

class cls = new class(); fixed(int* ptr1 = &cls.field1) //first field fixed(bool* ptr2 = &cls.field4) //requested field {     console.writeline((byte*)ptr2-(byte*)ptr1); } 

the resulting offset is, in case, 5, because runtime moves field3 end of type (and pads it), because type generic. know there marshal.offsetof, returns unmanaged offset, not managed.

how can retrieve offset fieldinfo instance? there .net method used that, or have write own, taking exceptions account (type size, padding, explicit offsets, etc.)?

with tricks around typedreference.maketypedreference, possible obtain reference field, , start of object's data, subtract. method can found in sharputils.


Comments

Popular posts from this blog

facebook - android ACTION_SEND to share with specific application only -

python - Creating a new virtualenv gives a permissions error -

javascript - cocos2d-js draw circle not instantly -