ObjectStateFormatter.Serialize 方法
定義
重要
部分資訊涉及發行前產品,在發行之前可能會有大幅修改。 Microsoft 對此處提供的資訊,不做任何明確或隱含的瑕疵擔保。
序列化物件狀態圖。
多載
Serialize(Object) |
將物件狀態圖序列化為 Base64 編碼字串。 |
Serialize(Stream, Object) |
將物件狀態圖序列化為指定的 Stream 物件。 |
Serialize(Object)
將物件狀態圖序列化為 Base64 編碼字串。
public:
System::String ^ Serialize(System::Object ^ stateGraph);
public string Serialize (object stateGraph);
member this.Serialize : obj -> string
Public Function Serialize (stateGraph As Object) As String
參數
- stateGraph
- Object
要序列化的物件。
傳回
Base-64 編碼字串,表示 stateGraph
參數的序列化物件狀態。
範例
下列程式碼範例示範如何使用 方法,將一組控制項屬性的值序列化為 base64 編碼字串 Serialize(Object) 。 字串可以在稍後使用 Deserialize(String) 方法還原序列化。
ArrayList controlProperties = new ArrayList(3);
controlProperties.Add( SortDirection );
controlProperties.Add( SelectedColumn );
controlProperties.Add( CurrentPage.ToString() );
// Create an ObjectStateFormatter to serialize the ArrayList.
ObjectStateFormatter formatter = new ObjectStateFormatter();
// Call the Serialize method to serialize the ArrayList to a Base64 encoded string.
string base64StateString = formatter.Serialize(controlProperties);
Dim controlProperties As New ArrayList(3)
controlProperties.Add(SortDirection)
controlProperties.Add(SelectedColumn)
controlProperties.Add(CurrentPage.ToString())
' Create an ObjectStateFormatter to serialize the ArrayList.
Dim formatter As New ObjectStateFormatter()
' Call the Serialize method to serialize the ArrayList to a Base64 encoded string.
Dim base64StateString As String = formatter.Serialize(controlProperties)
備註
使用 Serialize 方法序列化的任何物件圖形都可以使用 Deserialize 方法還原序列化。 方法 Serialize(Object) 用來將物件狀態圖形序列化為 base64 編碼字串形式。
適用於
Serialize(Stream, Object)
將物件狀態圖序列化為指定的 Stream 物件。
public:
void Serialize(System::IO::Stream ^ outputStream, System::Object ^ stateGraph);
public void Serialize (System.IO.Stream outputStream, object stateGraph);
member this.Serialize : System.IO.Stream * obj -> unit
Public Sub Serialize (outputStream As Stream, stateGraph As Object)
參數
- outputStream
- Stream
Stream,ObjectStateFormatter 序列化指定之物件狀態的目標。
- stateGraph
- Object
要序列化的物件。
例外狀況
指定的 outputStream
為 null
。
範例
下列程式碼範例示範類別如何使用 方法,擷取 ObjectStateFormatter 實例,將檢視狀態和控制狀態序列化至資料流程 Serialize(Stream, Object) 。 此程式碼範例是提供給 類別之較大範例的 PageStatePersister 一部分。
//
// 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 方法還原序列化。 方法 Serialize(Stream, Object) 可用來將物件狀態圖表序列化為二進位 Stream 物件。