当前位置: 移动技术网 > IT编程>开发语言>c# > C# dictionary to bytes and bytes convert to dictionary

C# dictionary to bytes and bytes convert to dictionary

2019年12月13日  | 移动技术网IT编程  | 我要评论
 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()

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网