ObjectStateFormatter クラス
アセンブリ: System.Web (system.web.dll 内)


ObjectStateFormatter クラスは、オブジェクトの状態グラフを圧縮形式でシリアル化したり逆シリアル化したりします。
ObjectStateFormatter は、ビューステートとコントロールの状態をシリアル化する目的で PageStatePersister クラスとその派生クラスによって使用されます。また、ASP.NET インフラストラクチャのさまざまな部分に対してオブジェクトの状態グラフ形式を提供する目的で LosFormatter クラスによっても使用されます。
ObjectStateFormatter クラスは、多くの共通 .NET Framework 参照型および定数をシリアル化し書式化するために最適化されます。最適化される型を次の表に示します。
また、従来の文字列型と文字列配列はそのままシリアル化されたバイナリ ライタに書き込まれたり、ライタから書き込まれたりしますが、内部の文字列テーブルを作成することにより、最適化される文字列もあります。文字列に関連付けられている TypeConverter オブジェクトがあったり、その文字列が実際には IndexedString クラスのインスタンスである場合に、これらのテーブルを使用して文字列が最適化されます。
前述の表に含まれていない型は、ISerializable インターフェイスを実装したり、SerializableAttribute 属性が付いている場合に、BinaryFormatter オブジェクトを使用してバイナリ シリアル化されます。ObjectStateFormatter クラスは、これらのシリアル化できる型に対しては最適化されません。
ObjectStateFormatter クラスがシリアル化できない型を検出した場合、ArgumentException 例外がスローされます。

PageStatePersister クラスの派生クラスから StateFormatter プロパティにアクセスして、ObjectStateFormatter インスタンスを取得し、ビューステートとコントロールの状態をシリアル化してストリームにする方法を次のコード例に示します。このコード例は、PageStatePersister クラスのトピックで取り上げているコード例の一部分です。
' ' 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 'Save
// // 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."); } }

System.Web.UI.ObjectStateFormatter


Windows 98, Windows 2000 SP4, Windows Server 2003, Windows XP Media Center Edition, Windows XP Professional x64 Edition, Windows XP SP2, Windows XP Starter Edition
開発プラットフォームの中には、.NET Framework によってサポートされていないバージョンがあります。サポートされているバージョンについては、「システム要件」を参照してください。


- ObjectStateFormatter クラスのページへのリンク