的BinaryFormatter中不存在CF.解决方案? [英] BinaryFormatter does not exist in CF. Solutions?

查看:210
本文介绍了的BinaryFormatter中不存在CF.解决方案?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要序列化/反序列化的obj在/从字节[]在紧凑的框架,但没有BinaryFormatter的,我应该怎么办? 谢谢。 这是我使用在服务器端类的,我想这也是在客户端(与设备的Windows Mobile 6)

 公共类串行器
{
    公共字节[] SerializeObject(obj对象)
    {
        如果(OBJ == NULL)
            返回null;
        使用(MemoryStream的流=新的MemoryStream())
        {
            BinaryFormatter的格式化=新的BinaryFormatter();
            formatter.Serialize(流OBJ);
            返回stream.ToArray();
        }
    }

    公共对象DeserializeObject(byte []的字节)
    {
        如果(字节== NULL)
            返回null;
        BinaryFormatter的格式化=新的BinaryFormatter();
        MemoryStream的流=新的MemoryStream(字节);
        返回formatter.Deserialize(流);
    }
}
 

解决方案

对于您的情况,则可以考虑的 protobuf网;这是不一样的格式为的BinaryFormatter ,所以客户端和服务器都需要调整,但它是一个二进制序列化的API在两个平台上的作品,而且是<一个href="http://stackoverflow.com/questions/3790728/performance-tests-of-serializations-used-by-wcf-bindings/3793091#3793091">typically小得多作为一个附加的额外费用。该格式实际上是谷歌的协议缓冲区编码;速度非常快,与平台无关,并设计为你添加属性等灵活而且它是免费的。一些傻瓜只是给它拿走。

I need to serialize/deserialize obj in/from byte[] on compact framework but there is no BinaryFormatter, what should i do? Thanks. This is the class i am using on the server side and i want it also on the client(a device with windows mobile 6)

public class Serializer
{
    public byte[] SerializeObject(object obj)
    {
        if (obj == null)
            return null;
        using (MemoryStream stream = new MemoryStream())
        {
            BinaryFormatter formatter = new BinaryFormatter();
            formatter.Serialize(stream, obj);
            return stream.ToArray();
        }
    }

    public object DeserializeObject(byte[] bytes)
    {
        if (bytes == null)
            return null;
        BinaryFormatter formatter = new BinaryFormatter();
        MemoryStream stream = new MemoryStream(bytes);
        return formatter.Deserialize(stream);
    }
}

解决方案

For your scenario, you might consider switching protobuf-net; this is not the same format as BinaryFormatter, so both client and server would need tweaks, but is a binary serialization API that works on both platforms, and is typically much smaller as an added extra. The format is actually google's "protocol buffers" encoding; very fast, platform-independent, and designed to be flexible as you add properties etc. And it is free. Some fool just gives it away.

这篇关于的BinaryFormatter中不存在CF.解决方案?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆