将C数据结构和typedef映射到.Net

Gre*_*ler 5 .net porting data-structures

我正在尝试编写一个用于与网络协议通信的接口,但IEEE文档描述了位级别的协议,信息分割为单个字节.

处理C typedef的最佳方法是什么?

typedef struct {
   Nibble transportSpecific;
   Enumeration4 messageType;
   UInteger4 versionPTP;
   UInteger16 messageLength;
   UInteger8 domainNumber;
   Octet flagField[2];
   Integer64 correctionfield;
   PortIdentity sourcePortIdentity;
   UInteger16 sequenceId;
   UInteger8 controlField;
   Integer8 logMessageInterval;
} MsgHeader;
Run Code Online (Sandbox Code Playgroud)

何时将兼容层移植到.Net?

jon*_*ham 1

尽管无法表示小于字节的值,但 FieldOffsetAttribute 可能对您有帮助。

我将使用一个字节来表示两个值以实现互操作目的,然后通过属性 getter 访问该值。

unsafe struct MsgHeader
{
    public Nibble transportSpecific;
    //Enumeration4 messageType;
    //UInteger4 versionPTP;
    // use byte as place holder for these two fields
    public byte union;
    public ushort messageLength;
    public byte domainNumber;
    public fixed byte flagField[2];
    public long correctionfield;
    public PortIdentity sourcePortIdentity;
    public ushort sequenceId;
    public byte controlField;
    public sbyte logMessageInterval;

    // access value of two fields via getters
    public byte messageType { get { return (byte)(union >> 4); } }
    public byte versionPTP { get { return (byte)(union & 0xF); } }
}
Run Code Online (Sandbox Code Playgroud)