Reputation: 11
see struct below
struct STRUCT_ITEM
{
short Index;
union
{
short Value;
struct
{
unsigned char Type;
unsigned char Values;
};
} Effect[3];
};
Hello .. Good evening. I would like if it is possible to convert a structure in C ++ to C #? I would like the new structure to contain the array of 3 objects, as you can see in the code in C ++.
Is to be used like this:
var Item = new Item ();
Item.Effect [2] .Type = 2;
I just get it that way:
[FieldOffset(0)]
public short Index;
[FieldOffset(2)]
public short MountHP;
[FieldOffset(2)]
public byte EF1;
[FieldOffset(3)]
public byte EFV1;
[FieldOffset(4)]
public byte EF2;
[FieldOffset(5)]
public byte EFV2;
[FieldOffset(6)]
public byte EF3;
[FieldOffset(7)]
public byte EFV3;
Is it possible to contain array in the structure? This structure contains the maximum size of 8 bytes.
Who can help, I am grateful! Thank you so much
Upvotes: 1
Views: 2198
Reputation: 139
Try this. and Yes, The array can use in Sturcture.
[StructLayout(LayoutKind.Explicit)]
public struct STRUCT_SUB_ITEM
{
[FieldOffset(0)]
public short Value;
[FieldOffset(0)]
public byte Type;
[FieldOffset(1)]
public byte Values;
}
[StructLayout(LayoutKind.Sequential)]
public struct STRUCT_ITEM
{
short index;
[MarshalAs(UnmanagedType.ByValArray, SizeConst = 3)]
public STRUCT_SUB_ITEM[] Effect;
}
and Test just like this.
static void test3()
{
STRUCT_ITEM item = new STRUCT_ITEM();
item.Effect = new STRUCT_SUB_ITEM[3];
item.Effect[0].Type = 1;
item.Effect[0].Values = 2;
item.Effect[1].Type = 1;
item.Effect[1].Values = 2;
item.Effect[2].Type = 1;
item.Effect[2].Values = 2;
Console.WriteLine(item.Effect[2].Value);
}
And an Array with restrict types (bool, int, double, ... ) Using Fixed keyword, it dosen't need alloc memory.
[StructLayout(LayoutKind.Explicit, CharSet = CharSet.Ansi)]
public unsafe struct STRUCT_ITEM2
{
[FieldOffset(0)]
public fixed byte item_01[260];
}
Upvotes: 2