我怎么能忽略一个字段的P / Invoke封送处理的结构时,字段、结构、我怎么能、Invoke

2023-09-04 09:29:58 作者:男人调情是天性‰

我要为元帅以P / Invoke的使用结构,但这种结构包含一个字段,只与我的管理code,所以我不希望它被封,因为它不属于在天然结构。它甚至有可能?我一直在寻找的序列相似的属性,以非序列化,但它似乎并不存在...

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;
}

任何建议将AP preciated

Any suggestion would be appreciated

推荐答案

有没有办法让CLR忽视的一个领域。我会转而使用两个结构,或许使一个在另一个的成员。

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; 
}