ObjectStateFormatter 类

定义

序列化和反序列化表示对象状态的对象图。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
继承
ObjectStateFormatter
实现

示例

下面的代码示例演示从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.

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 (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 类的新实例。Initializes a new instance of the ObjectStateFormatter class.

方法

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.

(继承自 Object)
GetHashCode()

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

(继承自 Object)
GetType()

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

(继承自 Object)
MemberwiseClone()

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

(继承自 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()

返回一个表示当前对象的 string。Returns a string that represents the current object.

(继承自 Object)

显式接口实现

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).

适用于