C# 用键值对来构建数据并将其序列化成字节数组 Key_Len_Data版本

以键值对的形式构建数据,并最终将其转成字节数组,浪费时间写的代码估计用不上了,也没多做测试,扔了可惜,保存一下,供以后学习研究

修改一个bug,BitConverter.GetBytes(val)没有sbyte和byte的重载,如果传入sbyte和byte的化,会默认调用short的重载,由于sbyte可以隐式转成short,编辑器不会提醒。。。

所以这就是为什么每个类型我都弄一个KV子类的原因了,KV_Int8,这样就不会粗心大意了吧

我重写了sbyte和byte的GetBytes函数,来解决无法给Bytes赋值的问题,虽然原本设计很怪,但是每个子类都能各施其职实现自己的功能就很不错

using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.IO;
using System.IO.MemoryMappedFiles;
using System.Linq;
using System.Security.Cryptography;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using Newtonsoft.Json;
using Newtonsoft.Json.Converters;
using PL;

namespace ConsoleApp {
    class Program {
        static void Main(string[] args) {
            try {
                Test();
            } catch (Exception ex) {
                Console.WriteLine(ex.ToString());
            }
            Console.WriteLine("end");
            Console.ReadKey();
        }
        static void Test() {
            BitConverter.GetBytes((byte)1);
            KV_Object kv = new KV_Object(0x00);
            kv.Add(new KV_Int32(0x00, 200));
            KV_Object kv1 = new KV_Object(0x01);
            kv.Add(kv1);
            kv1.Add(new KV_Int32(0x00, 255));
            List<byte> list = new List<byte>();
            kv.GetBytes(ref list);
            byte[] bytes = list.ToArray();
        }
        public abstract class KV {
            public byte Key { get; private set; }
            public byte[] Bytes { get; protected set; }
            public KV(byte key) {
                Key = key;
            }
            public virtual int GetBytes(ref List<byte> list) {
                list.Add(Key);
                list.AddRange(BitConverter.GetBytes(Bytes.Length).Reverse().ToArray());
                list.AddRange(Bytes);
                return 1 + 4 + Bytes.Length;
            }
        }
        public class KV_Object : KV {
            public List<KV> Val;
            public KV_Object(byte key) : base(key) {
                Val = new List<KV>();
            }
            public void Add(KV kv) {
                Val.Add(kv);
            }
            public override int GetBytes(ref List<byte> list) {
                list.Add(Key);
                int offset = list.Count;
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x00);
                int len = 0;
                foreach (KV kv in Val) {
                    len += kv.GetBytes(ref list);
                }
                byte[] lenBytes = BitConverter.GetBytes(len).Reverse().ToArray();
                list[offset++] = lenBytes[0];
                list[offset++] = lenBytes[1];
                list[offset++] = lenBytes[2];
                list[offset++] = lenBytes[3];
                return 1 + 4 + len;
            }
        }
        public class KV_Boolean : KV {
            public bool Val;
            public KV_Boolean(byte key, bool val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(Val);
            }
        }
        public class KV_Int8 : KV {
            public sbyte Val;
            public KV_Int8(byte key, sbyte val) : base(key) {
                Val = val;
            }
            public override int GetBytes(ref List<byte> list) {
                list.Add(Key);
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x01);
                list.Add((byte)Val);
                return 1 + 4 + 1;
            }
        }
        public class KV_UInt8 : KV {
            public byte Val;
            public KV_UInt8(byte key, byte val) : base(key) {
                Val = val;
            }
            public override int GetBytes(ref List<byte> list) {
                list.Add(Key);
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x00);
                list.Add(0x01);
                list.Add(Val);
                return 1 + 4 + 1;
            }
        }
        public class KV_Int16 : KV {
            public short Val;
            public KV_Int16(byte key, short val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(Val).Reverse().ToArray();
            }
        }
        public class KV_UInt16 : KV {
            public ushort Val;
            public KV_UInt16(byte key, ushort val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(Val).Reverse().ToArray();
            }
        }
        public class KV_Int32 : KV {
            public int Val;
            public KV_Int32(byte key, int val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_UInt32 : KV {
            public uint Val;
            public KV_UInt32(byte key, uint val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_Int64 : KV {
            public long Val;
            public KV_Int64(byte key, long val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_UInt64 : KV {
            public ulong Val;
            public KV_UInt64(byte key, ulong val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_Float : KV {
            public float Val;
            public KV_Float(byte key, float val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_Double : KV {
            public double Val;
            public KV_Double(byte key, double val) : base(key) {
                Val = val;
                Bytes = BitConverter.GetBytes(val).Reverse().ToArray();
            }
        }
        public class KV_ByteArray : KV {
            public byte[] Val;
            public KV_ByteArray(byte key, byte[] val) : base(key) {
                Val = val;
                Bytes = val;
            }
        }
        public class KV_String : KV {
            public string Val;
            public KV_String(byte key, string val) : base(key) {
                Val = val;
                Bytes = Encoding.UTF8.GetBytes(val);
            }
        }
    }
}

 

posted @ 2021-06-23 15:30  WmW  阅读(372)  评论(0编辑  收藏  举报