[Mono-list] Marshal.SizeOf() vs. sizeof operator
Thong (Tum) Nguyen
tum@veridicus.com
Wed, 13 Aug 2003 16:40:18 +1200
The type of FieldType.Undefined is "FieldType" not "byte".
You should prolly use Enum.GetUnderlyingType(typeof(FieldType)).
^Tum
> -----Original Message-----
> From: mono-list-admin@lists.ximian.com [mailto:mono-list-
> admin@lists.ximian.com] On Behalf Of Giuseppe Greco
> Sent: Wednesday, 13 August 2003 4:00 p.m.
> To: Marcus
> Cc: mono-list@ximian.com
> Subject: Re: [Mono-list] Marshal.SizeOf() vs. sizeof operator
>
> Marcus,
>
> here's an example:
>
> public enum FieldType : byte
> {
> Undefined = 0,
> Int16,
> Int32,
> Int64
> }
>
> Then, try something like this:
>
> using System;
> using System.Runtime.InteropServices;
>
> public class MyClass
> {
> private FieldType fieldType = FieldType.Undefined;
>
> public void MyMethod()
> {
> Console.WriteLine("The size of fieldType is {0}",
> Marshal.SizeOf(fieldType.GetType());
> }
> }
>
> The code above executes sucessfully with Mono on Linux,
> but it doesn't with .NET on Windwos, where I always get
> the following exception:
>
> Unhandled Exception: System.ArgumentException:
> Type FieldType can not be marshaled as an unmanaged
> structure; no meaningful size or offset can be computed.
> at System.Runtime.InteropServices.Marshal.SizeOf(Type t)
>
> Rewriting MyMethod like this
>
> public void MyMethod()
> {
> Console.WriteLine("The size of fieldType is {0}",
> Marshal.SizeOf(Type.GetType("System.Byte"));
> }
>
> it does function also with .NET on Windows...
>
> OK, my target is Mono on *nix, but I cannot assume that
> all the users of our software are running Linux...
>
> Thanks,
> Gius_.
>
>
> >-- Original Message --
> >From: Marcus <mathpup@mylinuxisp.com>
> >To: "Giuseppe Greco" <gius.greco@bluewin.ch>
> >Subject: Re: [Mono-list] Marshal.SizeOf() vs. sizeof operator
> >Cc: mono-list@ximian.com
> >Date: 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"
> >_______________________________________________
> >Mono-list maillist - Mono-list@lists.ximian.com
> >http://lists.ximian.com/mailman/listinfo/mono-list
>
> _______________________________________________
> Mono-list maillist - Mono-list@lists.ximian.com
> http://lists.ximian.com/mailman/listinfo/mono-list