vb.net - 在 .NET 中将结构转换为字节数组

标签 vb.net marshalling arrays

我希望使用 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

对 C# 中的编码还是新手,但我如何将此结构获取到字节数组以写入文件。是否有一些编码技巧或者我必须编写自定义方法?

最佳答案

使用.NET框架提供的序列化机制:

Dim formatter As New BinaryFormatter
formatter.Serialize(outputFileStream, objectInstance)

您应该添加<Serializable()>属性到您的类型。

关于vb.net - 在 .NET 中将结构转换为字节数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1288966/

相关文章:

c# - 在 c# 中将 uchar[] 从 native dll 编码到 byte[] 的正确方法

java - 按第一个单词对 ArrayList 进行排序

vb.net - DataGridComboBoxColumn 中的选择更改事件

php - 如何使用列名作为值并过滤最高值MYSQL/PHP

C# 编码回调

c# - 如何在没有不安全的情况下正确编码 C# 中的非托管数组

c++ - 生成编译时数组结构 (c++17)

php检查数组中的数字

asp.net - 将字典绑定(bind)到 GridView

vb.net - 更改pdfptable的字体大小