Possible Duplicate:
C++ union in C#可能重复:C#中的C ++联合
Code in C:
C中的代码:
typedef struct _EVENT_HEADER {
USHORT Size; // Event Size
USHORT HeaderType; // Header Type
USHORT Flags; // Flags
USHORT EventProperty; // User given event property
ULONG ThreadId; // Thread Id
ULONG ProcessId; // Process Id
LARGE_INTEGER TimeStamp; // Event Timestamp
GUID ProviderId; // Provider Id
EVENT_DESCRIPTOR EventDescriptor; // Event Descriptor
union {
struct {
ULONG KernelTime; // Kernel Mode CPU ticks
ULONG UserTime; // User mode CPU ticks
} DUMMYSTRUCTNAME;
ULONG64 ProcessorTime; // Processor Clock
// for private session events
} DUMMYUNIONNAME;
GUID ActivityId; // Activity Id
} EVENT_HEADER, *PEVENT_HEADER;
I converted anything but the union. How to convert it to C#?
除了工会,我改变了任何东西。如何将其转换为C#?
2 个解决方案
#1
1
C# doesn't natively support the C/C++ notion of unions. You can however use the StructLayout(LayoutKind.Explicit) and FieldOffset attributes to create equivalent functionality.
C#本身并不支持C / C ++的联合概念。但是,您可以使用StructLayout(LayoutKind.Explicit)和FieldOffset属性来创建等效功能。
Regarding to union
: in the code below you can see that Kernel
and ProcessorTime
have the same offset. LargeInteger is also a good example of union implementation in C#.
关于union:在下面的代码中,您可以看到Kernel和ProcessorTime具有相同的偏移量。 LargeInteger也是C#中联合实现的一个很好的例子。
EventHeader
[StructLayout(LayoutKind.Explicit)]
public struct EventHeader
{
[FieldOffset(0)]
public ushort Size;
[FieldOffset(2)]
public ushort HeaderType;
[FieldOffset(4)]
public ushort Flags;
[FieldOffset(6)]
public ushort EventProperty;
[FieldOffset(8)]
public uint ThreadId;
[FieldOffset(12)]
public uint ProcessId;
[FieldOffset(16)]
public LargeInteger TimeStamp;
[FieldOffset(24)]
public Guid ProviderId;
[FieldOffset(40)]
public Guid EventDescriptor;
[FieldOffset(52)]
public uint KernelTime;
[FieldOffset(56)]
public uint UserTime;
[FieldOffset(52)]
public ulong ProcessorTime;
[FieldOffset(60)]
public Guid ActivityId;
}
LargeInteger
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LargeInteger
{
[FieldOffset(0)]
public long QuadPart;
[FieldOffset(0)]
public uint LowPart;
[FieldOffset(4)]
public uint HighPart;
}
EventDescriptor
[StructLayout(LayoutKind.Sequential)]
public struct EventDescriptor
{
public ushort Id;
public byte Level;
public byte Channel;
public byte LevelSeverity;
public byte Opcode;
public ushort Task;
public uint Keyword;
}
Disclaimer: I just made this code. Didn't test it. The code may have errors.
免责声明:我刚刚制作了这段代码。没试过。代码可能有错误。
#2
3
You can use [StructLayout(LayoutKind.Explicit)] to explicitly place the members at the correct offsets.
您可以使用[StructLayout(LayoutKind.Explicit)]将成员显式放置在正确的偏移处。
Here is an example from an answer I provided previously
这是我之前提供的答案的一个例子
[StructLayout(LayoutKind.Explicit)]
public struct CharUnion
{
[FieldOffset(0)] public char UnicodeChar;
[FieldOffset(0)] public byte AsciiChar;
}
[StructLayout(LayoutKind.Explicit)]
public struct CharInfo
{
[FieldOffset(0)] public CharUnion Char;
[FieldOffset(2)] public short Attributes;
}
#1
1
C# doesn't natively support the C/C++ notion of unions. You can however use the StructLayout(LayoutKind.Explicit) and FieldOffset attributes to create equivalent functionality.
C#本身并不支持C / C ++的联合概念。但是,您可以使用StructLayout(LayoutKind.Explicit)和FieldOffset属性来创建等效功能。
Regarding to union
: in the code below you can see that Kernel
and ProcessorTime
have the same offset. LargeInteger is also a good example of union implementation in C#.
关于union:在下面的代码中,您可以看到Kernel和ProcessorTime具有相同的偏移量。 LargeInteger也是C#中联合实现的一个很好的例子。
EventHeader
[StructLayout(LayoutKind.Explicit)]
public struct EventHeader
{
[FieldOffset(0)]
public ushort Size;
[FieldOffset(2)]
public ushort HeaderType;
[FieldOffset(4)]
public ushort Flags;
[FieldOffset(6)]
public ushort EventProperty;
[FieldOffset(8)]
public uint ThreadId;
[FieldOffset(12)]
public uint ProcessId;
[FieldOffset(16)]
public LargeInteger TimeStamp;
[FieldOffset(24)]
public Guid ProviderId;
[FieldOffset(40)]
public Guid EventDescriptor;
[FieldOffset(52)]
public uint KernelTime;
[FieldOffset(56)]
public uint UserTime;
[FieldOffset(52)]
public ulong ProcessorTime;
[FieldOffset(60)]
public Guid ActivityId;
}
LargeInteger
[StructLayout(LayoutKind.Explicit, Size = 8)]
public struct LargeInteger
{
[FieldOffset(0)]
public long QuadPart;
[FieldOffset(0)]
public uint LowPart;
[FieldOffset(4)]
public uint HighPart;
}
EventDescriptor
[StructLayout(LayoutKind.Sequential)]
public struct EventDescriptor
{
public ushort Id;
public byte Level;
public byte Channel;
public byte LevelSeverity;
public byte Opcode;
public ushort Task;
public uint Keyword;
}
Disclaimer: I just made this code. Didn't test it. The code may have errors.
免责声明:我刚刚制作了这段代码。没试过。代码可能有错误。
#2
3
You can use [StructLayout(LayoutKind.Explicit)] to explicitly place the members at the correct offsets.
您可以使用[StructLayout(LayoutKind.Explicit)]将成员显式放置在正确的偏移处。
Here is an example from an answer I provided previously
这是我之前提供的答案的一个例子
[StructLayout(LayoutKind.Explicit)]
public struct CharUnion
{
[FieldOffset(0)] public char UnicodeChar;
[FieldOffset(0)] public byte AsciiChar;
}
[StructLayout(LayoutKind.Explicit)]
public struct CharInfo
{
[FieldOffset(0)] public CharUnion Char;
[FieldOffset(2)] public short Attributes;
}