IStateFormatter.Serialize(Object) 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
將 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 伺服器控制項檢視狀態的物件。
傳回
代表 Web 伺服器控制項檢視狀態的字串。
範例
下列程式碼範例示範方法如何將 Serialize 檢視狀態資訊保存到檔案。 類別 Save 的 StreamPageStatePersister
方法會使用 IStateFormatter 繼承自 類別的 PageStatePersister 介面來序列化檢視狀態。 此程式碼範例是介面所提供較大範例的 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 方法從字串重新建構狀態物件。