首页 新闻 会员 周边 捐助

求助如何把C++的typedef union 转换成C#的struct

0
悬赏园豆:50 [待解决问题]

typedef union _USB_PROTOCOLS {
ULONG ul;
struct {
ULONG Usb110 : 1;
ULONG Usb200 : 1;
ULONG Usb300 : 1;
ULONG ReservedMBZ : 29;
};
} USB_PROTOCOLS, *PUSB_PROTOCOLS;

如上,这一段转换成C#d结构体是什么样的?

lopengye的主页 lopengye | 初学一级 | 园豆:152
提问于:2019-11-09 10:46
< >
分享
所有回答(2)
0
2012 | 园豆:21645 (高人七级) | 2019-11-12 08:34
0

C语言的位域C#没法转换,不过可以换个思路用属性来模拟

public struct _USB_PROTOCOLS
{
    public UInt32 ul;

    public UInt32 Usb110
    {
        get => ul & 0x80000000;
        set => ul = (ul & 0x7FFFFFFF) | (value << 31);
    }

    public UInt32 Usb200
    {
        get => Usb110;
        set => Usb110 = value;
    }

    public UInt32 Usb300
    {
        get => Usb110;
        set => Usb110 = value;
    }

    public UInt32 ReservedMBZ
    {
        get => ul & 0x7FFFFFFF;
        set => ul = (ul & 0x80000000) | (value & 0x7FFFFFFF);
    }
}
sweetjian | 园豆:276 (菜鸟二级) | 2020-01-17 16:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册