IStateFormatter.Serialize(Object) 方法

定义

将 ASP.NET Web 服务器控件状态序列化为字符串形式。

public:
 System::String ^ Serialize(System::Object ^ state);
public string Serialize (object state);
abstract member Serialize : obj -> string
Public Function Serialize (state As Object) As String

参数

state
Object

该对象表示要序列化为字符串形式的 Web 服务器控件的视图状态。

返回

String

表示 Web 服务器控件的视图状态的字符串。

示例

下面的代码示例演示了 Serialize 该方法如何将视图状态信息保存到文件中。 类 Save 的方法 StreamPageStatePersister 使用 IStateFormatterPageStatePersister 类继承的接口来序列化视图状态。 此代码示例是为接口提供的大型示例的 IStateFormatter 一部分。

//
// Persist any ViewState and ControlState.
//
public override void Save()
{

    if (ViewState != null || ControlState != null)
    {
        if (Page.Session != null)
        {
            Stream stateStream = GetSecureStream();

            StreamWriter writer = new StreamWriter(stateStream);

            IStateFormatter formatter = this.StateFormatter;
            Pair statePair = new Pair(ViewState, ControlState);

            // Serialize the statePair object to a string.
            string serializedState = formatter.Serialize(statePair);

            writer.Write(serializedState);
            writer.Close();
            stateStream.Close();
        }
        else
        {
            throw new InvalidOperationException("Session needed for StreamPageStatePersister.");
        }
    }
}
'
' Persist any ViewState and ControlState.
'
Public Overrides Sub Save()

    If Not (ViewState Is Nothing) OrElse Not (ControlState Is Nothing) Then
        If Not (Page.Session Is Nothing) Then

            Dim stateStream As Stream
            stateStream = GetSecureStream()

            ' Write a state string, using the StateFormatter.
            Dim writer As New StreamWriter(stateStream)

            Dim formatter As IStateFormatter
            formatter = Me.StateFormatter

            Dim statePair As New Pair(ViewState, ControlState)

            Dim serializedState As String
            serializedState = formatter.Serialize(statePair)

            writer.Write(serializedState)
            writer.Close()
            stateStream.Close()
        Else
            Throw New InvalidOperationException("Session needed for StreamPageStatePersister.")
        End If
    End If
End Sub

注解

使用该方法将 Serialize 对象状态图转换为字符串形式。 使用 Deserialize 该方法从字符串中重新构建状态对象。

适用于