Marshal.SizeOf()

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Beringer

    Marshal.SizeOf()

    Why do I get the following run time error:

    Additional information: Type System.Object can not be marshaled as an
    unmanaged structure; no meaningful size or offset can be computed.

    When the following code is executed?
    object test = new object();

    int temp = Marshal.SizeOf( test);



    Isn't the size of test determined during its creation?



    Thanks,

    Eric


  • Willy Denoyette [MVP]

    #2
    Re: Marshal.SizeOf( )

    You can only use Marshal.SizeOf to get the size of a value type (struct) or
    a class that has an explicit layout declaration (StructLayoutAt tribute),
    none of the FCL classes have this attribute.

    Both....
    struct os
    {
    int i;
    }

    [StructLayout(La youtKind.Sequen tial)]
    class MyClass
    {
    int i;
    }

    will return 4, but using any FCL class will fail.
    Note that this method is only usable in interop scenario's where you may
    need the size of the (or to be) marshaled data portion of a class or
    struct.
    Do not use this to get the real size of the managed type however.

    Willy.

    "Beringer" <borden_eric@in valid.com> wrote in message
    news:tedLd.2382 $bu.264@fed1rea d06...[color=blue]
    > Why do I get the following run time error:
    >
    > Additional information: Type System.Object can not be marshaled as an
    > unmanaged structure; no meaningful size or offset can be computed.
    >
    > When the following code is executed?
    > object test = new object();
    >
    > int temp = Marshal.SizeOf( test);
    >
    >
    >
    > Isn't the size of test determined during its creation?
    >
    >
    >
    > Thanks,
    >
    > Eric
    >
    >[/color]


    Comment

    • Beringer

      #3
      Re: Marshal.SizeOf( )

      Thanks for the response.
      I later found this information after searching MSDN for awhile.
      Eric
      "Willy Denoyette [MVP]" <willy.denoyett e@pandora.be> wrote in message
      news:%23OsoEQ5B FHA.1408@TK2MSF TNGP10.phx.gbl. ..[color=blue]
      > You can only use Marshal.SizeOf to get the size of a value type (struct)
      > or a class that has an explicit layout declaration
      > (StructLayoutAt tribute), none of the FCL classes have this attribute.
      >
      > Both....
      > struct os
      > {
      > int i;
      > }
      >
      > [StructLayout(La youtKind.Sequen tial)]
      > class MyClass
      > {
      > int i;
      > }
      >
      > will return 4, but using any FCL class will fail.
      > Note that this method is only usable in interop scenario's where you may
      > need the size of the (or to be) marshaled data portion of a class or
      > struct.
      > Do not use this to get the real size of the managed type however.
      >
      > Willy.
      >
      > "Beringer" <borden_eric@in valid.com> wrote in message
      > news:tedLd.2382 $bu.264@fed1rea d06...[color=green]
      >> Why do I get the following run time error:
      >>
      >> Additional information: Type System.Object can not be marshaled as an
      >> unmanaged structure; no meaningful size or offset can be computed.
      >>
      >> When the following code is executed?
      >> object test = new object();
      >>
      >> int temp = Marshal.SizeOf( test);
      >>
      >>
      >>
      >> Isn't the size of test determined during its creation?
      >>
      >>
      >>
      >> Thanks,
      >>
      >> Eric
      >>
      >>[/color]
      >
      >[/color]


      Comment

      Working...