[Mono-list] Marshal.SizeOf() vs. sizeof operator
Tue, 12 Aug 2003 16:34:38 -0500
The sizeof operator can only be applied to valuetypes (e.g. char, int, struct)
and not to reference types (classes). The sizeof operator is only available
in unsafe code.
Marshal.SizeOf() can be applied to an instance or a Type, and it returns the
unmanaged size of the object. Unlike the sizeof operator, Marshal.SizeOf() is
not restricted to valuetypes.
I don't quite understand what you were saying about char types. Can you
provide an example?
As for custom classes, I suspect that you need to use the StructLayout
attribute, such as [StructLayout(LayoutKind.Sequential)] or using
explicit layout. The error on Rotor is System.ArgumentException: Type Class
can not be marshaled as an unmanaged structure; no meaningful size or offset
can be computed.
On Tuesday 12 August 2003 7:29 am, Giuseppe Greco wrote:
> Marshal.SizeOf() and the sizeof() operator
> return the same result, so what's the real
> difference between them?
> Until now, I've used Marshal.SizeOf()...
> and with Mono on Linux it works fine.
> This morning I've had the good (or bad) idea
> to test my code on Windows with .NET... and
> the result was
> 1. Marshal.SizeOf() is unable to determine the
> type of custom classes
> 2. for char types, Marshal.SizeOf() always returns
> "System.byte" instead of "System.Char"