kol
kol

Reputation: 28708

Read-only array field in unsafe struct

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).

Upvotes: 3

Views: 562

Answers (2)

kol
kol

Reputation: 28708

I found a solution, which is based on the idea of indexed properties:

[StructLayout(LayoutKind.Explicit, Size = 14)]
public struct YB
{
  [FieldOffset(0)] private fixed byte b[14];
  public byte this[int i] { get { fixed (byte* p = b) { return p[i]; } } }
}

[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct YC
{
  [FieldOffset(0)] private fixed byte c[8];
  public byte this[int i] { get { fixed (byte* p = c) { return p[i]; } } }
}

[StructLayout(LayoutKind.Explicit, Size = 24)]
public unsafe struct Y
{
  [FieldOffset(0)] private ushort a;
  [FieldOffset(2)] private YB b;
  [FieldOffset(16)] private YC c;

  public ushort A { get { return a; } }
  public YB B { get { return b; } }
  public YC C { get { return c; } }
};

Upvotes: 1

Chris
Chris

Reputation: 2895

You can use an indexer propertie to access the array at a particular index. Unfortunately it needs to be defined on the struct itself rather than B, but this should provide what you're looking for:

[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 this [int i]
    {
        get 
        {
            byte b1;
            fixed (byte* b2 = b)
            {
                b1 = b2[i];
            }
            return b1;
        }
    }
 }; 

Upvotes: 3

Related Questions