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
Returns
- object
结构体实例
BytesToStruct<T>(byte[])
将字节数组转换为指定结构体类型的实例
public static T BytesToStruct<T>(byte[] bytes) where T : struct
Parameters
bytesbyte[]源字节数组
Returns
- T
结构体实例
Type Parameters
T结构体类型
CopyStructToBytes(object?, byte[], int, int)
将结构体复制到字节数组的指定区域(支持偏移和截断)
public static void CopyStructToBytes(object? structValue, byte[] bytes, int offset, int size)
Parameters
GetBytes(object)
将值转换为字节数组
public static byte[] GetBytes(object value)
Parameters
valueobject原始值
Returns
- byte[]
对应的字节数组
Exceptions
GetStructSize(Type)
获取结构体总大小
public static int GetStructSize(Type structType)
Parameters
structTypeType结构体类型
Returns
- int
所需字节数
GetTypeByteLength(Type)
获取基础类型的字节长度
public static int GetTypeByteLength(Type type)
Parameters
typeType类型
Returns
- int
字节大小
Exceptions
GetValueFromBytes(byte[], Type, int)
从字节数组中提取指定类型的值
public static object GetValueFromBytes(byte[] bytes, Type targetType, int offset = 0)
Parameters
Returns
- object
对应类型的值
Exceptions
StructToBytes(object)
将结构体实例序列化为字节数组
public static byte[] StructToBytes(object structValue)
Parameters
structValueobject结构体实例
Returns
- byte[]
字节数组