ObjectStateFormatter ObjectStateFormatter ObjectStateFormatter ObjectStateFormatter Class

定義

將代表物件狀態的物件圖形序列化和還原序列化。Serializes and deserializes object graphs that represent the state of an object. 這個類別無法被繼承。This class cannot be inherited.

public ref class ObjectStateFormatter sealed : System::Runtime::Serialization::IFormatter
public sealed class ObjectStateFormatter : System.Runtime.Serialization.IFormatter
type ObjectStateFormatter = class
    interface IStateFormatter
    interface IFormatter
Public NotInheritable Class ObjectStateFormatter
Implements IFormatter
繼承
ObjectStateFormatterObjectStateFormatterObjectStateFormatterObjectStateFormatter
實作

範例

下列程式碼範例示範衍生自PageStatePersister類別的類別如何StateFormatter存取屬性來取得ObjectStateFormatter實例, 以將檢視狀態和控制項狀態序列化為數據流。The following code example demonstrates how a class that derives from the PageStatePersister class accesses the StateFormatter property to retrieve an ObjectStateFormatter instance to serialize view state and control state to a stream. 這個程式碼範例是針對PageStatePersister類別提供之較大範例的一部分。This code example is part of a larger example provided for the PageStatePersister class.

//
// 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

備註

ObjectStateFormatter類別會以精簡格式序列化和還原序列化物件狀態圖形。The ObjectStateFormatter class serializes and deserializes object state graphs in a compact format.

ObjectStateFormatter由衍生自它PageStatePersister的類別和類別所使用, 以序列化檢視狀態和控制項狀態。ObjectStateFormatter is used by the PageStatePersister class and classes that derive from it to serialize view state and control state. LosFormatter類別也會使用它來為 ASP.NET 基礎結構的各個部分提供物件狀態圖形格式。It is also used by the LosFormatter class to provide object state graph formatting for various parts of the ASP.NET infrastructure.

ObjectStateFormatter類別已優化, 可序列化和格式化許多常見的 .NET Framework 參考型別, 以及常數。The ObjectStateFormatter class is optimized to serialize and format many common .NET Framework reference types, as well as constants. 下表列出已優化的類型。The following table lists the types that are optimized.

Array DateTime Int16 String
ArrayList Double Int32 字串 []String []
Boolean Enum null (Nothing)null (Nothing) String.Empty
Byte Hashtable Pair Triplet
Char HybridDictionary Single Type
Color IDictionary

此外, 雖然傳統字串類型和字串陣列會原封不動地寫入序列化的二進位寫入器, 但某些字串會藉由建立內部字串資料表來進行優化。Additionally, while conventional string types and string arrays are written to and from a serialized binary writer unaltered, some strings are optimized by creating internal string tables. 如果字串具有相關聯TypeConverter的物件, 或字串實際上是IndexedString類別的實例, 則會使用這些資料表來優化字串。Strings are optimized using these tables if the string has an associated TypeConverter object or if the string is actually an instance of the IndexedString class.

上述未列出的其他類型是使用BinaryFormatter物件進行二進位序列化 (如果它們會ISerializable執行介面, SerializableAttribute或以屬性裝飾)。Other types not listed above are binary-serialized using a BinaryFormatter object if they implement the ISerializable interface or are decorated with the SerializableAttribute attribute. ObjectStateFormatter類別並未針對這些可序列化類型的任何一項優化。The ObjectStateFormatter class is not optimized for any of these serializable types.

如果類別遇到不可序列化的類型ArgumentException , 則會擲回例外狀況。 ObjectStateFormatterIf the ObjectStateFormatter class encounters a type that is not serializable, an ArgumentException exception is thrown.

建構函式

ObjectStateFormatter() ObjectStateFormatter() ObjectStateFormatter() ObjectStateFormatter()

初始化 ObjectStateFormatter 類別的新執行個體。Initializes a new instance of the ObjectStateFormatter class.

方法

Deserialize(Stream) Deserialize(Stream) Deserialize(Stream) Deserialize(Stream)

將包含在指定之 Stream 物件的二進位序列化形式的物件狀態圖還原序列化。Deserializes an object state graph from its binary-serialized form that is contained in the specified Stream object.

Deserialize(String) Deserialize(String) Deserialize(String) Deserialize(String)

將序列化 Base64 編碼字串形式的物件狀態圖還原序列化。Deserializes an object state graph from its serialized base64-encoded string form.

Equals(Object) Equals(Object) Equals(Object) Equals(Object)

判斷指定的物件是否等於目前的物件。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode() GetHashCode() GetHashCode() GetHashCode()

做為預設雜湊函式。Serves as the default hash function.

(Inherited from Object)
GetType() GetType() GetType() GetType()

取得目前執行個體的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone() MemberwiseClone() MemberwiseClone() MemberwiseClone()

建立目前 Object 的淺層複本 (Shallow Copy)。Creates a shallow copy of the current Object.

(Inherited from Object)
Serialize(Object) Serialize(Object) Serialize(Object) Serialize(Object)

將物件狀態圖序列化為 Base64 編碼字串。Serializes an object state graph to a base64-encoded string.

Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object) Serialize(Stream, Object)

將物件狀態圖序列化為指定的 Stream 物件。Serializes an object state graph to the specified Stream object.

ToString() ToString() ToString() ToString()

傳回代表目前物件的字串。Returns a string that represents the current object.

(Inherited from Object)

明確介面實作

IFormatter.Binder IFormatter.Binder IFormatter.Binder IFormatter.Binder

如需這個成員的說明,請參閱 BinderFor a description of this member, see Binder.

IFormatter.Context IFormatter.Context IFormatter.Context IFormatter.Context

如需這個成員的說明,請參閱 ContextFor a description of this member, see Context.

IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream) IFormatter.Deserialize(Stream)

如需這個成員的說明,請參閱 Deserialize(Stream)For a description of this member, see Deserialize(Stream).

IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object) IFormatter.Serialize(Stream, Object)

如需這個成員的說明,請參閱 Serialize(Stream, Object)For a description of this member, see Serialize(Stream, Object).

IFormatter.SurrogateSelector IFormatter.SurrogateSelector IFormatter.SurrogateSelector IFormatter.SurrogateSelector

如需這個成員的說明,請參閱 SurrogateSelectorFor a description of this member, see SurrogateSelector.

IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String) IStateFormatter.Deserialize(String)

如需這個成員的說明,請參閱 Deserialize(String)For a description of this member, see Deserialize(String).

IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object) IStateFormatter.Serialize(Object)

如需這個成員的說明,請參閱 Serialize(Object)For a description of this member, see Serialize(Object).

適用於