ObjectLayoutInspector icon indicating copy to clipboard operation
ObjectLayoutInspector copied to clipboard

add SizeOfInstanceFields for Unsafe

Open dzmitry-lahoda opened this issue 4 years ago • 4 comments

SizeOf != SizeOfInstance Fields. needed for networking.

dzmitry-lahoda avatar Feb 11 '21 15:02 dzmitry-lahoda

Could you please clarify the issue a bit more?

SergeyTeplyakov avatar Feb 11 '21 17:02 SergeyTeplyakov

What is size of this struct, 5 or 8?

    [StructLayout(LayoutKind.Explicit)]
    public struct ChallengeRequest
    {
        public static ChallengeRequest Create()
        {
            return new ChallengeRequest();
        }

        [FieldOffset(0)]
        public RequestMessageType Type;

        /// <summary>
        /// 0 (using a u32 here since that pads the request to the same size as theresponse)
        /// </summary>
        [FieldOffset(1)]
        public uint Padding;
    }

C# sizeof and Unsafe.SizeOf will tell 8, but I want 5. And not using reflection or runtime codegen.

dzmitry-lahoda avatar Feb 11 '21 18:02 dzmitry-lahoda

Ok. Now I get it. Thanks. Will fix.

SergeyTeplyakov avatar Feb 11 '21 21:02 SergeyTeplyakov

I am not sure if reflection based stuff have or not have that type of API, but unsafe based stuff (which works within AOT), do not have that for sure. But anyway, because it lacked in unsafe part, calculated manually.

dzmitry-lahoda avatar Feb 12 '21 05:02 dzmitry-lahoda