Marshal a class partially

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • marcosegurini@gmail.com

    Marshal a class partially

    Hi.

    Is is possible to mark a class-member-variable to avoid its marshaling?

    [StructLayout(La youtKind.Sequen tial)]
    class MyClass
    {
    int i_;
    IntPtr point_;

    object obj; // this should not be marshaled
    }

    TIA.
    Marco.

  • Tasos Vogiatzoglou

    #2
    Re: Marshal a class partially

    Use [NotSerialized] attribute.

    Comment

    • marcosegurini@gmail.com

      #3
      Re: Marshal a class partially

      Thanks Tasos

      but testing your suggestion with the following code it seems wrong:

      //

      using System;
      using System.Collecti ons.Generic;
      using System.Text;
      using System.Runtime. InteropServices ;

      namespace NotMarshal
      {
      class Program
      {
      [StructLayout(La youtKind.Sequen tial)]
      struct MyStruct
      {
      int i;
      int j;
      int k;
      }

      [StructLayout(La youtKind.Sequen tial)]
      struct MyStruct1
      {
      int i;
      [NonSerialized]
      int j;
      int k;
      }

      [StructLayout(La youtKind.Sequen tial)]
      struct MyStruct2
      {
      int i;
      int k;
      }

      static void Main(string[] args)
      {
      System.Console. WriteLine("Size Of " + typeof(MyStruct ).Name
      + " = " + Marshal.SizeOf( typeof(MyStruct )));
      System.Console. WriteLine("Size Of " + typeof(MyStruct 1).Name
      + " = " + Marshal.SizeOf( typeof(MyStruct 1)));
      System.Console. WriteLine("Size Of " + typeof(MyStruct 2).Name
      + " = " + Marshal.SizeOf( typeof(MyStruct 2)));
      }
      }
      }

      // output

      SizeOf MyStruct = 12
      SizeOf MyStruct1 = 12
      SizeOf MyStruct2 = 8

      sizeof MyStruct1 has to be 8.

      Comment

      • Mattias Sjögren

        #4
        Re: Marshal a class partially

        >Is is possible to mark a class-member-variable to avoid its marshaling?

        No. Can't you just separate the members to different types?


        Mattias

        --
        Mattias Sjögren [C# MVP] mattias @ mvps.org
        http://www.msjogren.net/dotnet/ | http://www.dotnetinterop.com
        Please reply only to the newsgroup.

        Comment

        • marcosegurini@gmail.com

          #5
          Re: Marshal a class partially

          it's what I did.

          Thanks.

          Comment

          Working...