-
Notifications
You must be signed in to change notification settings - Fork 4.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Unsafe.OffsetOf to get field offsets in types #43285
Comments
Basically dupe of #28001. Btw while we are on this topic i would also like overload based on Edit: This is what im talking about: https://stackoverflow.com/questions/30817924/obtain-non-explicit-field-offset Interestingly enough it still works on net 5 Edit: You already included such overload but i think my link is still useful for those who cant wait or as reference for possible impl Just a trivia this method work for private fields too which is useful for source generators |
Or |
Other methods like e.g. |
Unsafe is all about unsafe pointer manipulation (by looks of it). |
Yes but these byte offsets are only ever useful when used with unsafe operations like the example provided in the issue. |
IMO #23716 is a better way to handle this. We really shouldn't be encouraging devs to perform arbitrary interior pointer manipulation on CLR objects which have been unsafe cast to an invalid / dummy type. There's no stated guarantee that the CLR supports such manipulation. |
Would the new Roslyn code generator extensions not cover much of these requirements? |
@obiwanjacobi as far as i know you cant access private data with source generators currently unless u can link IL code somehow with source generator (important for databinding+serializers) since we dont have blessed reflection-free way to do it (offsetof on |
@BreyerW Source generators generate a |
@Joe4evr right forgot about that however to be fully useable we must get relaxed Edit scratch that i was remembering wrong it seems we still need some sort of offsetof functionality since source generators are insufficient due to |
Closing; assuming #45152 supersedes this. |
Background and Motivation
We wanted to quickly set attribute-tagged fields in user defined types at runtime. The fastest and most flexible way to do this would be various
Unsafe
APIs likeAddByteOffset
andAs
to getref
s to the field and to then set them (obviously caching the field offsets ahead of time). The problem is that it's kind of extremely annoying to get the actual offset of individual fields: the best way I could figure out is usingDynamicMethod
to generate a tiny method that returns aref
to the field or the offset directly (because there is noFieldInfo.GetValueRef
or something), then toUnsafe.ByteOffset<T>
it.So I simply propose an API that gets the in-memory offset of a field from the object's memory layout.
Proposed API
Usage Example
Some nuisances
There isn't really a "clean" way to get a base reference this field offset can be used with. You can define a new class with a single
byte
field and then get a ref to that (afterUnsafe.As
) but that's far from clear or obvious.So maybe an API to actually fetch the base reference for an object would go along with this aswell.
The text was updated successfully, but these errors were encountered: