如何反序列化对象

如何反序列化对象

本文介绍了如果我不知道正确的类型,如何反序列化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在客户端和服务器之间发送命令和数据。

Well I want to send commands and data between client and server.

我有三个项目:


  • 客户

  • 服务器

  • 公用-在这里,我放置了通用类和网络抽象层

我在客户端和服务器之间使用以下数据结构进行通信

I am using following data structures for communications between client and server

public class Packet<T>
{
        public string Name { get; set; }
        public string From { get; set; }
        public string To { get; set; }
        public PacketType PacketType { get; set; }
        public T Container { get; set; }

        public Packet()
        {
        }

        public Packet(string name, PacketType packetType, T container)
        {
            Name = name;
            PacketType = packetType;
            Container = container;
        }
    }

    public enum PacketType
    {
        Command,
        Data
    }

如果我需要发送有关文件的信息,我只创建一个具有必要结构的数据包 CreatePacket< FilesInfo>(filesInfo ),然后对其进行序列化并将其发送到client\server。

If I need to send information about files I just create a packet with the necessary structure CreatePacket<FilesInfo>(filesInfo) and then serialize it and send it to client\server.

但是我如何在接收方反序列化数据?我不知道数据包是什么对象类型。是否有其他方法或库或其他东西可以解决我的问题?另外,我也不想使用WCF,因为我的应用程序可以在安装了.NET 2.0的计算机上运行。

But how I can deserialize data on receiving side? I don't know what is object type the packet is. Is there any other way or library or something to solve my problem? Also I don't want to use WCF because my app should work on machines with .NET 2.0 installed.

推荐答案

chrfin和haiyyu都一样并且是个好主意。下面是使用Packet Base类在构造中保存类型数据的细微差别。您序列化Packet< T>并反序列化为数据包。然后,使用非常简单。如前所述,技巧仍然是使Type易于访问。

Both chrfin and haiyyu have the same and a good idea. Below is the minor twist of using a Packet Base class to hold your type data on construction. You serialize a Packet< T > and deserialize to a Packet. Use is pretty simple then. Still the trick as already mentioned is to make the Type easily accessible.

class Program
{
    static void Main(string[] args)
    {
        var pack = new Packet<int>() { Payload = 13 };
        var serializedData = pack.Serialize();
        var deserializedData = Packet.Deserialize(serializedData);
        Console.WriteLine("The payload type is:" + deserializedData.PayloadType.Name);
        Console.WriteLine("the payload is: " + deserializedData.Payload);
        Console.ReadLine();
    }
}



[Serializable]
public class Packet
{
    public Type PayloadType { get; protected set; }
    public object Payload { get; protected set; }
    public static Packet Deserialize(byte[] bytes)
    {
        return (Packet)(new BinaryFormatter()).Deserialize(new MemoryStream(bytes));
    }
}

[Serializable]
class Packet<T> : Packet
{
    public Packet()
    {
        PayloadType = typeof(T);
    }
    public new T Payload
    {
        get { return (T)base.Payload; }
        set { base.Payload = value; }
    }

    public override string ToString()
    {
        return "[Packet]" + Payload.ToString();
    }

    public byte[] Serialize()
    {
        MemoryStream m = new MemoryStream();
        (new BinaryFormatter()).Serialize(m, this);
        return m.ToArray();
    }
}

这篇关于如果我不知道正确的类型,如何反序列化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-05 08:56