Exemplo n.º 1
0
    /// <summary>
    /// 演示SoapFormatter的序列化和反序列化
    /// </summary>
    void ShowSoapFormatter()
    {
        var soapFormatterOjbect = new API.SoapFormatterOjbect {
            ID = Guid.NewGuid(), Name = "ShowSoapFormatter", Age = 28, Time = DateTime.Now
        };

        var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

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

        formatter.Serialize(ms, soapFormatterOjbect);

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

        txtSoapFormatter.Text = str;


        // 反序列化
        var buffer = System.Text.Encoding.UTF8.GetBytes(str);
        var ms2    = new MemoryStream(buffer);
        var soapFormatterOjbect2 = formatter.Deserialize(ms2) as API.SoapFormatterOjbect;

        lblSoapFormatter.Text = soapFormatterOjbect2.Name;
    }
Exemplo n.º 2
0
    /// <summary>
    /// 演示SoapFormatter的序列化和反序列化
    /// </summary>
    void ShowSoapFormatter()
    {
        var soapFormatterOjbect = new API.SoapFormatterOjbect { ID = Guid.NewGuid(), Name = "ShowSoapFormatter", Age = 28, Time = DateTime.Now };

        var formatter = new System.Runtime.Serialization.Formatters.Soap.SoapFormatter();

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

        formatter.Serialize(ms, soapFormatterOjbect);

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

        // 反序列化
        var buffer = System.Text.Encoding.UTF8.GetBytes(str);
        var ms2 = new MemoryStream(buffer);
        var soapFormatterOjbect2 = formatter.Deserialize(ms2) as API.SoapFormatterOjbect;
        lblSoapFormatter.Text = soapFormatterOjbect2.Name;
    }