Marshal a class partially

  • Thread starter Thread starter marcosegurini
  • Start date Start date
M

marcosegurini

Hi.

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

[StructLayout(LayoutKind.Sequential)]
class MyClass
{
int i_;
IntPtr point_;

object obj; // this should not be marshaled
}

TIA.
Marco.
 
Thanks Tasos

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

//

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.InteropServices;

namespace NotMarshal
{
class Program
{
[StructLayout(LayoutKind.Sequential)]
struct MyStruct
{
int i;
int j;
int k;
}

[StructLayout(LayoutKind.Sequential)]
struct MyStruct1
{
int i;
[NonSerialized]
int j;
int k;
}

[StructLayout(LayoutKind.Sequential)]
struct MyStruct2
{
int i;
int k;
}

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

// output

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

sizeof MyStruct1 has to be 8.
 
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
 
Back
Top