Table of Contents

Class StructBinaryHelper

Namespace
MAS.Communication
Assembly
MAS.Communication.dll

结构体与字节数组的通用编解码

public static class StructBinaryHelper
Inheritance
StructBinaryHelper
Inherited Members

Methods

BytesToStruct(byte[], Type)

将字节数组转换为结构体实例

public static object BytesToStruct(byte[] bytes, Type structType)

Parameters

bytes byte[]

源字节数组

structType Type

目标结构体类型

Returns

object

结构体实例

BytesToStruct<T>(byte[])

将字节数组转换为指定结构体类型的实例

public static T BytesToStruct<T>(byte[] bytes) where T : struct

Parameters

bytes byte[]

源字节数组

Returns

T

结构体实例

Type Parameters

T

结构体类型

CopyStructToBytes(object?, byte[], int, int)

将结构体复制到字节数组的指定区域(支持偏移和截断)

public static void CopyStructToBytes(object? structValue, byte[] bytes, int offset, int size)

Parameters

structValue object

结构体实例,null 时清空目标区域

bytes byte[]

目标字节数组

offset int

起始偏移

size int

复制长度

GetBytes(object)

将值转换为字节数组

public static byte[] GetBytes(object value)

Parameters

value object

原始值

Returns

byte[]

对应的字节数组

Exceptions

NotSupportedException

GetStructSize(Type)

获取结构体总大小

public static int GetStructSize(Type structType)

Parameters

structType Type

结构体类型

Returns

int

所需字节数

GetTypeByteLength(Type)

获取基础类型的字节长度

public static int GetTypeByteLength(Type type)

Parameters

type Type

类型

Returns

int

字节大小

Exceptions

NotSupportedException

GetValueFromBytes(byte[], Type, int)

从字节数组中提取指定类型的值

public static object GetValueFromBytes(byte[] bytes, Type targetType, int offset = 0)

Parameters

bytes byte[]

源字节数组

targetType Type

目标类型

offset int

起始偏移

Returns

object

对应类型的值

Exceptions

NotSupportedException

StructToBytes(object)

将结构体实例序列化为字节数组

public static byte[] StructToBytes(object structValue)

Parameters

structValue object

结构体实例

Returns

byte[]

字节数组