I'm writing an UDP client which is receiving complex data structures. I do have the structures in C++, but my program is in C#
There are a lot of different structures with Bitfiels and unions.
Is there any way that I do not have to convert the structs by hand?
Also is there an easy way to implement Bitfields and Unions in C#?
Right now I'm using Properties for Bitfields, but that's some difficult work, with a strong possibility for mistakes.
I provided an simplified example what I'm doing right now, there are about 50 structs with 100 lines of code each.
Example c++:
typedef struct Message_s
{
unsigned char var : 2;
unsigned char var2 : 6;
union
{
struct
{
unsigned char sVar;
unsigned char sVar2;
}
char arr[32];
}
}Message_t;
Example C#:
[System.Runtime.InteropServices.StructLayout(System.Runtime.InteropServices.LayoutKind.Sequential)]
struct Message_s
{
private byte Field1
public byte var
{
get
{
return (byte)(Field1 & 0x03);
}
set
{
Field1 = (byte)((Field1 & ~0x03) | value);
}
public byte var2
{
get
{
return (byte)(Field1 & 0xFC);
}
set
{
Field1 = (byte)((Field1 & 0x03) | value<<2);
}
}
//unions may be possible with properties...
}