IStateFormatter Rozhraní

Definice

Definuje metody, které typ implementuje pro serializaci a deserializaci objektového grafu.

public interface class IStateFormatter
public interface IStateFormatter
type IStateFormatter = interface
Public Interface IStateFormatter
Odvozené

Příklady

Následující příklad kódu ukazuje, jak vytvořit PageStatePersister objekt, který ukládá zobrazení a stav řízení do datového proudu na webovém serveru. Třída StreamPageStatePersister ukazuje, jak přepsat Load a Save metody extrahovat a uložit informace o stavu stránky. Tyto metody používají IStateFormatter rozhraní zděděné z PageStatePersister třídy k serializaci a deserializaci stavu zobrazení. Tento příklad kódu je součástí většího příkladu poskytnutého PageStatePersister pro třídu.

namespace Samples.AspNet.CS
{

    using System;
    using System.IO;
    using System.Security.Permissions;
    using System.Web;
    using System.Web.UI;

    //
    // The StreamPageStatePersister is an example view state
    // persistence mechanism that persists view and control
    // state on the Web server.
    //
    [AspNetHostingPermission(SecurityAction.Demand, Level = AspNetHostingPermissionLevel.Minimal)]
    public class StreamPageStatePersister : PageStatePersister
    {

        public StreamPageStatePersister(Page page)
            : base(page)
        {
        }
        //
        // Load ViewState and ControlState.
        //
        public override void Load()
        {
            Stream stateStream = GetSecureStream();

            // Read the state string, using the StateFormatter.
            StreamReader reader = new StreamReader(stateStream);

            IStateFormatter formatter = this.StateFormatter;
            string fileContents = reader.ReadToEnd();

            // Deserilize returns the Pair object that is serialized in
            // the Save method.
            Pair statePair = (Pair)formatter.Deserialize(fileContents);

            ViewState = statePair.First;
            ControlState = statePair.Second;
            reader.Close();
            stateStream.Close();
        }
        //
        // 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.");
                }
            }
        }
        // Return a secure Stream for your environment.
        private Stream GetSecureStream()
        {
            // You must provide the implementation to build
            // a secure Stream for your environment.
            return null;
        }
    }
}
Imports System.IO
Imports System.Security.Permissions
Imports System.Web
Imports System.Web.UI

Namespace Samples.AspNet.VB

    ' The StreamPageStatePersister is an example view state
    ' persistence mechanism that persists view and control
    ' state on the Web server.
    '
    <AspNetHostingPermission(SecurityAction.Demand, Level:=AspNetHostingPermissionLevel.Minimal)> _
    Public Class StreamPageStatePersister
        Inherits PageStatePersister


        Public Sub New(ByVal page As Page)
            MyBase.New(page)
        End Sub

        '
        ' Load ViewState and ControlState.
        '
        Public Overrides Sub Load()

            Dim stateStream As Stream
            stateStream = GetSecureStream()

            ' Read the state string, using the StateFormatter.
            Dim reader As New StreamReader(stateStream)

            Dim serializedStatePair As String
            serializedStatePair = reader.ReadToEnd
            Dim statePair As Pair

            Dim formatter As IStateFormatter
            formatter = Me.StateFormatter

            ' Deserilize returns the Pair object that is serialized in
            ' the Save method.      
            statePair = CType(formatter.Deserialize(serializedStatePair), Pair)

            ViewState = statePair.First
            ControlState = statePair.Second
            reader.Close()
            stateStream.Close()
        End Sub

        '
        ' 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
        ' Return a secure Stream for your environment.
        Private Function GetSecureStream() As Stream
            ' You must provide the implementation to build
            ' a secure Stream for your environment.
            Return Nothing
        End Function
    End Class
End Namespace

Poznámky

Rozhraní IStateFormatter definuje metody, které typ může implementovat serializovat a deserializovat stav, který ASP.NET webový server ovládací prvek udržuje ve své ViewState vlastnosti. Tato infrastruktura je používána třídami, které jsou odvozeny od PageStatePersister třídy k zachování stavu stránky ASP.NET mezi požadavky. Ve výchozím nastavení je ASP.NET stav stránky serializován a deserializován instancí ObjectStateFormatter třídy. Vývojáři webu a adaptéru však můžou implementovat IStateFormatter rozhraní podle svých vlastních typů k provedení této práce.

Další informace o správě stavu a zobrazení stavu řízení webového serveru najdete v tématu ASP.NET Přehled správy stavu a dynamické ovládací prvky webového serveru a stav zobrazení.

Metody

Deserialize(String)

Deserializuje graf stavu objektu z jeho serializované řetězcové formy.

Serialize(Object)

Serializuje ASP.NET stav ovládacího prvku webového serveru na formulář řetězce.

Platí pro

Viz také