typedef struct tagMarket {
union {
WORD m_wMarket;
char m_EShortName[3];
};
char m_Name[16];
char m_CShortName[5];
unsigned long m_lProperty;
unsigned long m_lDate;
short m_PeriodCount;
short m_OpenTime[5];
short m_CloseTime[5];
short m_nCount;
StockInfo m_Siif[1];
} SCMarket;请教,有如上一个C++中的Struct,包含一个union,如何在C#中定义?
{
int m_wMarket;
[(3)]
byte[] m_EShortName;
}当成struct 理论上应该是可以的。
用指针直接用byte然后再自己组装应该也可以。
public struct MyUnion
{
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public ushort m_wMarket;
[System.Runtime.InteropServices.MarshalAsAttribute(System.Runtime.InteropServices.UnmanagedType.ByValArray, SizeConst=3, ArraySubType=System.Runtime.InteropServices.UnmanagedType.I1)]
[System.Runtime.InteropServices.FieldOffsetAttribute(0)]
public byte[] m_EShortName;
}
unsafe struct tagMarket
{
public fixed sbyte m_EShortName[4];//这个字段代替union
public fixed sbyte m_Name[16];
public fixed sbyte m_CShortName[5];
public uint m_lProperty;
public uint m_lDate;
public short m_PeriodCount;
public fixed short m_OpenTime[5];
public fixed short m_CloseTime[5];
public short m_nCount;
public StockInfo m_Siif[1];
//下面的方法代替m_wMarket读写
public void writeet(ushort et)
{
//////
}
public ushort getet()
{
///////
}
}估计得写成这个样子,m_EShortName定义四个字节才能保证对齐。