SwDevMan81
SwDevMan81

Reputation: 50038

How do I ignore a field size in a struct using Marshal.SizeOf?

Is there a way to ignore a field in the calculated of the struct size using Marshal.SizeOf

Ex:

public struct Message
{
   public ushort X;
   public ushort Y; // Ignore this field in the calculation
}

int size = Marshal.SizeOf(typeof(Message));

Right now size is 4. I want the size to be 2. Is there a way to do this?

Upvotes: 2

Views: 1475

Answers (2)

JP Alioto
JP Alioto

Reputation: 45127

The only way I can think of doing it would be to create a Custom Marshaller and when you implement ICustomMarshaller.GetNativeDataSize, return 0. You would use MarshalAsAttribute to apply the custom marshaller to just that field. But, it won't marshal properly, so I don't know why you would want to do that.

Upvotes: 2

Yvo
Yvo

Reputation: 19273

I don't think that's possible. Why would you want to do that?

Upvotes: 0

Related Questions