Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I ignore a field when marshalling a structure with P/Invoke

I want to marshal a structure for use with P/Invoke, but this struct contains a field that is only relevant to my managed code, so I don't want it to be marshaled since it doesn't belong in the native structure. Is it even possible ? I was looking for an attribute similar to NonSerialized for serialization, but it doesn't seem to exist...

struct MyStructure
{
    int foo;
    int bar;

    [NotMarshaled] // This attribute doesn't exist, but that's the kind of thing I'm looking for...
    int ignored;
}

Any suggestion would be appreciated

like image 898
Thomas Levesque Avatar asked Nov 09 '09 22:11

Thomas Levesque


1 Answers

There's no way to make the CLR ignore a field. I would instead use two structures, and perhaps make one a member of the other.

struct MyNativeStructure 
{ 
    public int foo; 
    public int bar; 
} 

struct MyStructure 
{ 
    public MyNativeStruct native; 
    public int ignored; 
} 
like image 169
Mattias S Avatar answered Oct 05 '22 17:10

Mattias S