How to convert a structure to a byte array in C#?

前端 未结 14 1081
难免孤独
难免孤独 2020-11-22 09:59

How do I convert a structure to a byte array in C#?

I have defined a structure like this:

public struct CIFSPacket
{
    public uint protocolIdentifi         


        
14条回答
  •  眼角桃花
    2020-11-22 10:17

    Looks like a predefined (C level) structure for some external library. Marshal is your friend. Check:

    http://geekswithblogs.net/taylorrich/archive/2006/08/21/88665.aspx

    for a starter how to deal with this. Note that you can - with attributes - define things like byte layout and string handling. VERY nice approach, actually.

    Neither BinaryFormatter Nor MemoryStream are done for that.

提交回复
热议问题