This is the original declaration:
[StructLayout(LayoutKind.Explicit, Size = 16)]
public unsafe struct X
{
[FieldOffset(0)] public ushort a;
[FieldOffset(2)] public fixed byte b[14];
};
I would like to make the struct
read-only, but I don't know how I should write a getter for the array. The only solution I could figure out is a getter method:
[StructLayout(LayoutKind.Explicit, Size = 16)]
public unsafe struct X
{
[FieldOffset(0)] private ushort a;
[FieldOffset(2)] private fixed byte b[14];
public ushort A { get { return a; } }
public byte B(int i) { fixed (byte* p = b) { return p[i]; } }
};
Is it possible to write a getter property for b instead of a getter method?
=== UPDATE ===
I would also like to handle the case when there are more than one array fields. For example:
[StructLayout(LayoutKind.Explicit, Size = 24)]
public unsafe struct Y
{
[FieldOffset(0)] private ushort a;
[FieldOffset(2)] private fixed byte b[14];
[FieldOffset(16)] private fixed byte c[8];
public ushort A { get { return a; } }
public byte B(int i) { fixed (byte* p = b) { return p[i]; } }
public byte C(int i) { fixed (byte* p = c) { return p[i]; } }
};
Is it possible to write a getter properties for b and c instead of getter methods? I would like to write y.B[i]
and y.C[i]
instead of y.B(i)
and y.C(i)
.