use*_*350 3 vb.net bytearray marshalling
我希望使用My.Computer.FileSystem.WriteAllBytes等将由固定长度字符串组成的结构写入文件.
我使用的VB6项目具有固定长度的字符串,我已将其转换为VB.Net.
Structure Record
<VBFixedString(22),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=22)> Public tim() As Char
<VBFixedString(130),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=130)> Public des() As Char
<VBFixedString(2),System.Runtime.InteropServices.MarshalAs(System.Runtime.InteropServices.UnmanagedType.ByValArray,SizeConst:=2)> Public crlf() As Char
End Structure
Run Code Online (Sandbox Code Playgroud)
在C#中编组仍然是新手,但是我如何将这个结构转换为一个字节数组来写入文件.是否有一些编组技巧或我是否必须编写自定义方法?
使用.NET框架提供的序列化机制:
Dim formatter As New BinaryFormatter
formatter.Serialize(outputFileStream, objectInstance)
Run Code Online (Sandbox Code Playgroud)
您应该<Serializable()>为您的类型添加属性.