Beispiel #1
0
    /// <summary>
    /// 演示BinaryFormatter的序列化和反序列化
    /// </summary>
    void ShowBinaryFormatter()
    {
        var binaryFormatterObject = new API.BinaryFormatterObject {
            ID = Guid.NewGuid(), Name = "BinaryFormatter", Age = 28, Time = DateTime.Now
        };

        var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // 序列化
        var ms = new MemoryStream();

        formatter.Serialize(ms, binaryFormatterObject);

        ms.Position = 0;
        var buffer = ms.GetBuffer();
        var str    = System.Text.Encoding.UTF8.GetString(buffer);

        txtBinaryFormatter.Text = str;


        // 反序列化
        var ms2 = new MemoryStream(buffer);
        var binaryFormatterObject2 = formatter.Deserialize(ms2) as API.BinaryFormatterObject;

        lblBinaryFormatter.Text = binaryFormatterObject2.Name;
    }
Beispiel #2
0
    /// <summary>
    /// 演示BinaryFormatter的序列化和反序列化
    /// </summary>
    void ShowBinaryFormatter()
    {
        var binaryFormatterObject = new API.BinaryFormatterObject { ID = Guid.NewGuid(), Name = "BinaryFormatter", Age = 28, Time = DateTime.Now };

        var formatter = new System.Runtime.Serialization.Formatters.Binary.BinaryFormatter();

        // 序列化
        var ms = new MemoryStream();

        formatter.Serialize(ms, binaryFormatterObject);

        ms.Position = 0;
        var buffer = ms.GetBuffer();
        var str = System.Text.Encoding.UTF8.GetString(buffer);
        txtBinaryFormatter.Text = str;

        // 反序列化
        var ms2 = new MemoryStream(buffer);
        var binaryFormatterObject2 = formatter.Deserialize(ms2) as API.BinaryFormatterObject;
        lblBinaryFormatter.Text = binaryFormatterObject2.Name;
    }