C# dictionary to bytes and bytes convert to dictionary

2022-10-12,,

 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()

《C# dictionary to bytes and bytes convert to dictionary.doc》

下载本文的Word格式文档,以方便收藏与打印。