有关指定实体属性转换字节位置来转换字节数组

实体

	[Serializable]
    public class Person
    {
        [BinarySerialization(0, 4)]
        public int Id { get; set; }

        [BinarySerialization(4, 16)]
        public string Name { get; set; }

        [BinarySerialization(20, 4)]
        public int Age { get; set; }

        [BinarySerialization(24, 8)]
        public DateTime Birthday { get; set; }

        public override string ToString() => $"Id: {Id}, Name: {Name}, Age: {Age}, Birthday: {Birthday}";
    }

注解部分

 	[AttributeUsage(AttributeTargets.Property, AllowMultiple = false)]
    public class BinarySerializationAttribute : Attribute
    {
        public int Offset { get; }
        public int Length { get; }

        public BinarySerializationAttribute(int offset, int length)
        {
            Offset = offset;
            Length = length;
        }
    }

主要转换方法

 public static class ByteArrayExtensions
    {
        public static T Deserialize<T>(this byte[] bytes) where T : new()
        {
            if (bytes == null) throw new ArgumentNullException(nameof(bytes));

            T entity = new T();

            foreach (PropertyInfo property in typeof(T).GetProperties())
            {
                BinarySerializationAttribute attribute = property.GetCustomAttribute<BinarySerializationAttribute>();

                if (attribute != null)
                {
                    int offset = attribute.Offset;
                    int length = attribute.Length;

                    byte[] values = bytes.Skip(offset).Take(length).ToArray();

                    if (property.PropertyType == typeof(Int32))
                    {
                        int value = BitConverter.ToInt32(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(String))
                    {
                        string value = Encoding.UTF8.GetString(values);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(DateTime))
                    {
                        long value = BitConverter.ToInt64(values, 0);
                        property.SetValue(entity, new DateTime(value));
                    }
                    else if (property.PropertyType == typeof(Decimal))
                    {
                        int[] bits = new int[4];

                        for (int i = 0; i < bits.Length; i++)
                        {
                            byte[] data = new byte[4];
                            Array.Copy(values, i * 4, data, 0, 4);
                            bits[i] = BitConverter.ToInt32(data, 0);
                        }

                        property.SetValue(entity, new Decimal(bits));
                    }
                    else if (property.PropertyType == typeof(Byte))
                    {
                        property.SetValue(entity, values[0]);
                    }
                    else if (property.PropertyType == typeof(Double))
                    {
                        double value = BitConverter.ToDouble(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(Int16))
                    {
                        short value = BitConverter.ToInt16(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(Int64))
                    {
                        long value = BitConverter.ToInt64(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(Single))
                    {
                        float value = BitConverter.ToSingle(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(UInt16))
                    {
                        ushort value = BitConverter.ToUInt16(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(UInt32))
                    {
                        uint value = BitConverter.ToUInt32(values, 0);
                        property.SetValue(entity, value);
                    }
                    else if (property.PropertyType == typeof(UInt64))
                    {
                        ulong value = BitConverter.ToUInt64(values, 0);
                        property.SetValue(entity, value);
                    }
                }
            }

            return entity;
        }
    }

使用就是

 byte[] bytes = { 0x01, 0x00, 0x00, 0x00, 0xE8, 0xAF, 0xAD, 0xE5, 0x85, 0xB3, 0xE8, 0xAF, 0x95, 0xE5, 0x9D, 0x80, 0x5E, 0x5B, 0x4E, 0x61, 0x0, 0x0, 0x0, 0x0, 0x19, 0x0, 0x0, 0x0, 0x3, 0x5, 0x1, 0x1, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0, 0x0 };
 Person person = bytes.Deserialize<Person>();
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值