🔧 CoreExtend 扩展方法
命名空间: Snet.Core.extend | 类: CoreExtend (静态) | 源码: Snet.Core/extend/CoreExtend.cs
CoreExtend 提供 ConcurrentDictionary<string, AddressValue> 上的扩展方法集合,用于数据转换和序列化。
AddressValue 集合扩展
GetSimplifyArray — 转为精简传输数组
public static IEnumerable<AddressValueSimplify>? GetSimplifyArray(
this ConcurrentDictionary<string, AddressValue> param)
将地址值集合转为 AddressValueSimplify 轻量级传输模型数组。用于网络传输前的数据压缩。
GetSimplifyProtobuf — 转为 Protobuf 传输包
public static AddressValueSimplifyProtobuf? GetSimplifyProtobuf(
this ConcurrentDictionary<string, AddressValue> param)
包装为 Protobuf 优化的 AddressValueSimplifyProtobuf 批量传输模型。
SelectSimplifyArray / SelectSimplifyProtobuf — 按地址名筛选
public static IEnumerable<AddressValueSimplify>? SelectSimplifyArray(
this ConcurrentDictionary<string, AddressValue> param, List<string> addressNames)
public static AddressValueSimplifyProtobuf? SelectSimplifyProtobuf(
this ConcurrentDictionary<string, AddressValue> param, List<string> addressNames)
仅转换指定地址名的数据,用于按需传输。
GetArray / SelectArray — 获取原始 AddressValue 数组
public static IEnumerable<AddressValue>? GetArray(
this ConcurrentDictionary<string, AddressValue> param)
public static IEnumerable<AddressValue>? SelectArray(
this ConcurrentDictionary<string, AddressValue> param, List<string> addressNames)
直接获取地址值集合(不转换),支持全部或筛选。
字节转换扩展
WriteGetBytes — 数值转字节数组
public static byte[] WriteGetBytes(this object value) // int16/uint16/int32/uint32/int64/uint64/double/single
public static byte[] WriteGetBytes(this string value, Encoding encoding)
将 C# 类型转为字节数组(网络字节序)。
ReadGetLength — DataType 对应字节长度
public static ushort ReadGetLength(this DataType dataType)
返回数据类型对应的单元素字节数。例如 Int16 → 2, Float → 4, Double → 8。
编码扩展
GetEncoding — EncodingType 转 Encoding
public static Encoding GetEncoding(this EncodingType? encodingType_AllowEmpty)
public static Encoding GetEncoding(this EncodingType encodingType)
将 Snet 的 EncodingType 枚举转为 .NET Encoding 对象。可空参数版本:null 返回 null。
GetDefaultEncodingWrite — 为 object 字典添加默认编码
public static ConcurrentDictionary<string, (object value, EncodingType? encodingType)>
GetDefaultEncodingWrite(this ConcurrentDictionary<string, object> sources,
EncodingType? encodingType = EncodingType.ANSI)
将 ConcurrentDictionary<string, object> 转为带 EncodingType 元组的字典。用于 WriteAsync 的便捷重载委托。
其他
MessageStandard — 消息标准化
public static string MessageStandard(this string input)
对消息字符串进行标准化处理(如去除多余空白、统一格式)。
CheckAddress
public static bool CheckAddress(this Address address)
验证地址集合中不存在无效数据(空地址名)。
