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
参数的序列化对象状态。
示例
下面的代码示例演示如何使用 Serialize(Object) 该方法将一组控件属性的值序列化为 base64 编码的字符串。 稍后可以使用该方法反序列化 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 对象。