欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  IT编程

C# dictionary to bytes and bytes convert to dictionary

程序员文章站 2022-05-29 09:19:51
static byte[] GetBytesFromDic(Dictionary dic) { if(dic==null || !dic.Any()) { return null; } using (MemoryStream ms = new MemoryStream( ......
 static byte[] getbytesfromdic(dictionary<string,string> dic)
        {
            if(dic==null || !dic.any())
            {
                return null;
            }
            using (memorystream ms = new memorystream())
            {
                binaryformatter binformatter = new binaryformatter();
                binformatter.serialize(ms, dic);
                byte[] dicbytes = ms.toarray();           
                return dicbytes;
            }
        }

        static dictionary<string,string> getdicfrombytes(byte[] dicbytes)
        {
            dictionary<string, string> dic = new dictionary<string, string>();
            using (memorystream ms = new memorystream(dicbytes))
            {
                binaryformatter binformatter = new binaryformatter();
                dic= (dictionary<string,string>) binformatter.deserialize(ms);
            }
            return dic;
        }

memorystream ms=new memorystream(bytes);

ms.toarray()