CF中不存在BinaryFormatter.解决方案?

gig*_*igi 8 .net c# compact-framework

我需要在紧凑框架上从/ []中序列化/反序列化obj,但是没有BinaryFormatter,我该怎么办?谢谢.这是我在服务器端使用的类,我也想在客户端(使用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);
    }
}
Run Code Online (Sandbox Code Playgroud)

Mar*_*ell 10

对于您的场景,您可以考虑切换protobuf-net ; 这与格式不同BinaryFormatter,因此客户端和服务器都需要调整,但它是一个可在两个平台上运行的二进制序列化API,并且通常小得多,作为额外的附加功能.格式实际上是google的"协议缓冲区"编码; 非常快速,独立于平台,并且在添加属性等时设计灵活.它是免费的.有些傻瓜只是放弃了.