Skip to content
Discussion options

You must be logged in to vote

Marshal.SizeOf<T>() is intended to get the size of the object after it's been marshalled and so is effectively the number of bytes that native sees.

sizeof(T) (and Unsafe.SizeOf<T>() is the size of the object in managed, which will match the size for blittable types.

Historically, generics were not supported in marshalling and so could not be passed across the P/Invoke boundary, this was changed around .NET Core 3.1, but many of the Marshal APIs were not updated to account for that as the built-in marshalling was already being considered "legacy" and was being looked at getting replaced (see https://learn.microsoft.com/en-us/dotnet/api/system.runtime.compilerservices.disableruntimemarshal…

Replies: 2 comments 2 replies

Comment options

You must be logged in to vote
2 replies
@AaronRobinsonMSFT
Comment options

@colejohnson66
Comment options

Answer selected by colejohnson66
Comment options

You must be logged in to vote
0 replies
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Category
Q&A
Labels
None yet
4 participants