ObjectStateFormatter Class

Definition

序列化和反序列化表示对象状态的对象图。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, System::Web::UI::IStateFormatter
public ref class ObjectStateFormatter sealed : System::Runtime::Serialization::IFormatter
public sealed class ObjectStateFormatter : System.Runtime.Serialization.IFormatter, System.Web.UI.IStateFormatter
public sealed class ObjectStateFormatter : System.Runtime.Serialization.IFormatter
type ObjectStateFormatter = class
    interface IStateFormatter
    interface IFormatter
Public NotInheritable Class ObjectStateFormatter
Implements IFormatter, IStateFormatter
Public NotInheritable Class ObjectStateFormatter
Implements IFormatter
Inheritance
ObjectStateFormatter
Implements

Examples

下面的代码示例演示派生自 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

Remarks

ObjectStateFormatter 类将对象状态图形序列化和反序列化为精简格式。The ObjectStateFormatter class serializes and deserializes object state graphs in a compact format.

ObjectStateFormatterPageStatePersister 类和派生自它的类用于序列化视图状态和控件状态。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 []String []
Boolean Enum null (Nothingnull (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.

如果 ObjectStateFormatter 类遇到不可序列化的类型,则会引发 ArgumentException 异常。If the ObjectStateFormatter class encounters a type that is not serializable, an ArgumentException exception is thrown.

Constructors

ObjectStateFormatter()

初始化 ObjectStateFormatter 类的新实例。Initializes a new instance of the ObjectStateFormatter class.

Methods

Deserialize(Stream)

从包含在指定的 Stream 对象中的对象状态图的二进制序列化形式对该对象状态图进行反序列化。Deserializes an object state graph from its binary-serialized form that is contained in the specified Stream object.

Deserialize(String)

从对象状态图的序列化 Base64 编码字符串形式对该对象状态图进行反序列化。Deserializes an object state graph from its serialized base64-encoded string form.

Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(Inherited from Object)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(Inherited from Object)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(Inherited from Object)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(Inherited from Object)
Serialize(Object)

将对象状态图序列化为 Base64 编码字符串。Serializes an object state graph to a base64-encoded string.

Serialize(Stream, Object)

将对象状态图序列化为指定的 Stream 对象。Serializes an object state graph to the specified Stream object.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(Inherited from Object)

Explicit Interface Implementations

IFormatter.Binder

有关此成员的说明,请参见 BinderFor a description of this member, see Binder.

IFormatter.Context

有关此成员的说明,请参见 ContextFor a description of this member, see Context.

IFormatter.Deserialize(Stream)

有关此成员的说明,请参见 Deserialize(Stream)For a description of this member, see Deserialize(Stream).

IFormatter.Serialize(Stream, Object)

有关此成员的说明,请参见 Serialize(Stream, Object)For a description of this member, see Serialize(Stream, Object).

IFormatter.SurrogateSelector

有关此成员的说明,请参见 SurrogateSelectorFor a description of this member, see SurrogateSelector.

IStateFormatter.Deserialize(String)

有关此成员的说明,请参见 Deserialize(String)For a description of this member, see Deserialize(String).

IStateFormatter.Serialize(Object)

有关此成员的说明,请参见 Serialize(Object)For a description of this member, see Serialize(Object).

Applies to