StateManagedCollection 클래스

정의

IStateManager 개체를 관리하는 모든 강력한 형식의 컬렉션에 대한 기본 클래스를 제공합니다.

public ref class StateManagedCollection abstract : System::Collections::IList, System::Web::UI::IStateManager
public abstract class StateManagedCollection : System.Collections.IList, System.Web.UI.IStateManager
type StateManagedCollection = class
    interface IList
    interface ICollection
    interface IEnumerable
    interface IStateManager
Public MustInherit Class StateManagedCollection
Implements IList, IStateManager
상속
StateManagedCollection
파생
구현

예제

다음 코드 예제에서 강력한 형식의 컬렉션 클래스를 파생 하는 방법을 보여 줍니다 StateManagedCollection 포함할 IStateManager 개체입니다. 이 예제에서는 합니다 CycleCollection 추상의 인스턴스를 포함 하도록 파생 Cycle 일 수 있는 클래스 Bicycle 또는 Tricycle 개체입니다. Cycle 클래스가 구현 하는 IStateManager 의 값을 저장 하기 때문에 인터페이스를 CycleColor 를 뷰 상태 속성.

namespace Samples.AspNet.CS.Controls {

    using System;
    using System.Security.Permissions;
    using System.Collections;
    using System.ComponentModel;
    using System.Drawing;           
    using System.Web;
    using System.Web.UI;            
    //////////////////////////////////////////////////////////////
    //
    // The strongly typed CycleCollection class is a collection
    // that contains Cycle class instances, which implement the
    // IStateManager interface.
    //
    //////////////////////////////////////////////////////////////
    [AspNetHostingPermission(SecurityAction.Demand, 
        Level=AspNetHostingPermissionLevel.Minimal)]
    public sealed class CycleCollection : StateManagedCollection {
        
        private static readonly Type[] _typesOfCycles 
            = new Type[] { typeof(Bicycle), typeof(Tricycle) };

        protected override object CreateKnownType(int index) {
            switch(index) {
                case 0:
                    return new Bicycle();
                case 1:
                    return new Tricycle();                    
                default:
                    throw new ArgumentOutOfRangeException("Unknown Type");
            }            
        }

        protected override Type[] GetKnownTypes() {
            return _typesOfCycles;
        }

        protected override void SetDirtyObject(object o) {
            ((Cycle)o).SetDirty();
        }
    }
    //////////////////////////////////////////////////////////////
    //
    // The abstract Cycle class represents bicycles and tricycles.
    //
    //////////////////////////////////////////////////////////////
    public abstract class Cycle : IStateManager {

        protected internal Cycle(int numWheels) : this(numWheels, "Red"){ }
        
        protected internal Cycle(int numWheels, String color) {    
            numberOfWheels = numWheels;
            CycleColor = color;
        }
        
        private int numberOfWheels = 0;
        public int NumberOfWheels {
            get { return numberOfWheels; }
        }
        
        public string CycleColor {
            get { 
                object o = ViewState["Color"];
                return (null == o) ? String.Empty : o.ToString() ;
            }
            set {
                ViewState["Color"] = value;            
            }        
        }

        internal void SetDirty() {
            ViewState.SetDirty(true);
        }
        
        // Because Cycle does not derive from Control, it does not 
        // have access to an inherited view state StateBag object.
        private StateBag viewState;
        private StateBag ViewState {
            get {
                if (viewState == null) {
                    viewState = new StateBag(false);
                    if (isTrackingViewState) {
                        ((IStateManager)viewState).TrackViewState();
                    }
                }
                return viewState;
            }
        }

        // The IStateManager implementation.
        private bool isTrackingViewState;
        bool IStateManager.IsTrackingViewState {
            get {
                return isTrackingViewState;
            }
        }

        void IStateManager.LoadViewState(object savedState) {
            object[] cycleState = (object[]) savedState;
            
            // In SaveViewState, an array of one element is created.
            // Therefore, if the array passed to LoadViewState has 
            // more than one element, it is invalid.
            if (cycleState.Length != 1) {
                throw new ArgumentException("Invalid Cycle View State");
            }
            
            // Call LoadViewState on the StateBag object.
            ((IStateManager)ViewState).LoadViewState(cycleState[0]);
        }

        // Save the view state by calling the StateBag's SaveViewState
        // method.
        object IStateManager.SaveViewState() {
            object[] cycleState = new object[1];

            if (viewState != null) {
                cycleState[0] = ((IStateManager)viewState).SaveViewState();
            }
            return cycleState;
        }

        // Begin tracking view state. Check the private variable, because 
        // if the view state has not been accessed or set, then it is not  
        // being used and there is no reason to store any view state.
        void IStateManager.TrackViewState() {
            isTrackingViewState = true;
            if (viewState != null) {
                ((IStateManager)viewState).TrackViewState();
            }
        }        
    }

    public sealed class Bicycle : Cycle {
    
        // Create a red Cycle with two wheels.
        public Bicycle() : base(2) {}    
    }
    
    public sealed class Tricycle : Cycle {
    
        // Create a red Cycle with three wheels.
        public Tricycle() : base(3) {}
    }
}
Imports System.Security.Permissions
Imports System.Collections
Imports System.ComponentModel
Imports System.Drawing
Imports System.Web
Imports System.Web.UI
 
Namespace Samples.AspNet.VB.Controls
    '////////////////////////////////////////////////////////////
    '
    ' The strongly typed CycleCollection class is a collection
    ' that contains Cycle class instances, which implement the
    ' IStateManager interface.
    '
    '////////////////////////////////////////////////////////////
    <AspNetHostingPermission(SecurityAction.Demand, _
        Level:=AspNetHostingPermissionLevel.Minimal)> _
                   Public NotInheritable Class CycleCollection
        Inherits StateManagedCollection

        Private Shared _typesOfCycles() As Type = _
            {GetType(Bicycle), GetType(Tricycle)}

        Protected Overrides Function CreateKnownType(ByVal index As Integer) As Object
            Select Case index
                Case 0
                    Return New Bicycle()
                Case 1
                    Return New Tricycle()
                Case Else
                    Throw New ArgumentOutOfRangeException("Unknown Type")
            End Select

        End Function


        Protected Overrides Function GetKnownTypes() As Type()
            Return _typesOfCycles

        End Function


        Protected Overrides Sub SetDirtyObject(ByVal o As Object)
            CType(o, Cycle).SetDirty()

        End Sub
    End Class
    '////////////////////////////////////////////////////////////
    '
    ' The abstract Cycle class represents bicycles and tricycles.
    '
    '////////////////////////////////////////////////////////////

    MustInherit Public Class Cycle
        Implements IStateManager


        Friend Protected Sub New(ByVal numWheels As Integer) 
            MyClass.New(numWheels, "Red")

        End Sub

        Friend Protected Sub New(ByVal numWheels As Integer, ByVal color As String) 
            numOfWheels = numWheels
            CycleColor = color

        End Sub

        Private numOfWheels As Integer = 0    
        Public ReadOnly Property NumberOfWheels() As Integer 
            Get
                Return numOfWheels
            End Get
        End Property 

        Public Property CycleColor() As String 
            Get
                Dim o As Object = ViewState("Color")
                If o Is Nothing Then 
                    Return String.Empty
                Else  
                    Return o.ToString()
                End If            
            End Get
            Set
                ViewState("Color") = value
            End Set
        End Property


        Friend Sub SetDirty() 
            ViewState.SetDirty(True)

        End Sub

        ' Because Cycle does not derive from Control, it does not 
        ' have access to an inherited view state StateBag object.
        Private cycleViewState As StateBag

        Private ReadOnly Property ViewState() As StateBag 
            Get
                If cycleViewState Is Nothing Then
                    cycleViewState = New StateBag(False)
                    If trackingViewState Then
                        CType(cycleViewState, IStateManager).TrackViewState()
                    End If
                End If
                Return cycleViewState
            End Get
        End Property

        ' The IStateManager implementation.
        Private trackingViewState As Boolean

        ReadOnly Property IsTrackingViewState() As Boolean _
            Implements IStateManager.IsTrackingViewState
            Get
                Return trackingViewState
            End Get
        End Property


        Sub LoadViewState(ByVal savedState As Object) _
            Implements IStateManager.LoadViewState
            Dim cycleState As Object() = CType(savedState, Object())

            ' In SaveViewState, an array of one element is created.
            ' Therefore, if the array passed to LoadViewState has 
            ' more than one element, it is invalid.
            If cycleState.Length <> 1 Then
                Throw New ArgumentException("Invalid Cycle View State")
            End If

            ' Call LoadViewState on the StateBag object.
            CType(ViewState, IStateManager).LoadViewState(cycleState(0))

        End Sub


        ' Save the view state by calling the StateBag's SaveViewState
        ' method.
        Function SaveViewState() As Object Implements IStateManager.SaveViewState
            Dim cycleState(0) As Object

            If Not (cycleViewState Is Nothing) Then
                cycleState(0) = _
                CType(cycleViewState, IStateManager).SaveViewState()
            End If
            Return cycleState

        End Function


        ' Begin tracking view state. Check the private variable, because 
        ' if the view state has not been accessed or set, then it is not being 
        ' used and there is no reason to store any view state.
        Sub TrackViewState() Implements IStateManager.TrackViewState
            trackingViewState = True
            If Not (cycleViewState Is Nothing) Then
                CType(cycleViewState, IStateManager).TrackViewState()
            End If

        End Sub
    End Class


    Public NotInheritable Class Bicycle
        Inherits Cycle


        ' Create a red Cycle with two wheels.
        Public Sub New()
            MyBase.New(2)

        End Sub
    End Class

    Public NotInheritable Class Tricycle
        Inherits Cycle


        ' Create a red Cycle with three wheels.
        Public Sub New()
            MyBase.New(3)

        End Sub
    End Class
End Namespace

설명

합니다 StateManagedCollection 클래스는 저장 하는 모든 강력한 형식의 컬렉션에 대 한 기본 클래스 IStateManager 요소를 포함 하 여 DataControlFieldCollection, ParameterCollectionStyleCollection, TreeNodeBindingCollection, 등입니다. StateManagedCollection 컬렉션 포함 된 요소의 상태 뿐만 아니라 자체의 상태를 관리 합니다. 따라서 호출 IStateManager.SaveViewState 컬렉션 상태 및 현재 컬렉션에 포함 된 모든 요소의 상태를 저장 합니다.

파생 하는 경우를 고려해 야 할 가장 중요 한 메서드를 StateManagedCollection 클래스 CreateKnownTypeGetKnownTypes, OnValidateSetDirty, 및 SetDirtyObject합니다. 합니다 CreateKnownTypeGetKnownTypes 메서드 뷰 상태에 포함 된 요소의 형식에 대 한 인덱스를 저장 하는 데 사용 됩니다. 정규화 된 형식 이름 대신 인덱스 저장 성능을 향상 시킵니다. OnValidate 메서드 컬렉션의 요소를 조작할 때마다 호출 됩니다 비즈니스 규칙에 따라 요소를 유효성을 검사 합니다. 그러나 현재 구현 된 OnValidate 메서드 금지 null 컬렉션에 저장 되는 개체 파생된 된 형식에서 사용자 고유의 유효성 검사 동작을 정의 하려면이 메서드를 재정의할 수 있습니다. SetDirty 메서드를 뷰 상태를 serialize 할 전체 컬렉션을 사용 하면, 마지막 시간 이후 상태에 대 한 변경만 직렬화 하는 대신 로드 합니다. SetDirtyObject 메서드는 추상 메서드이며 요소 수준에서이 동작을 수행 하기 위해 구현할 수 있습니다.

중요

StateManagedCollection 저장소 어셈블리의 정규화 된 뷰 상태에 컬렉션 항목의 이름을 입력합니다. 사이트 방문자를 뷰 상태를 디코딩할 하 고 형식 이름을 검색할 수 있습니다. 이 시나리오는 보안상 문제가 웹 사이트에서을 만드는 경우 뷰 상태에 배치 하기 전에 수동으로 형식 이름의 암호화할 수 있습니다.

생성자

StateManagedCollection()

StateManagedCollection 클래스의 새 인스턴스를 초기화합니다.

속성

Count

StateManagedCollection 컬렉션에 포함된 요소의 개수를 가져옵니다.

메서드

Clear()

StateManagedCollection 컬렉션에서 모든 항목을 제거합니다.

CopyTo(Array, Int32)

특정 배열 인덱스부터 StateManagedCollection 컬렉션의 요소를 배열에 복사합니다.

CreateKnownType(Int32)

파생 클래스에서 재정의된 경우 IStateManager를 구현하는 클래스의 인스턴스를 만듭니다. 만들어진 개체의 형식은 GetKnownTypes() 메서드에서 반환된 컬렉션의 지정된 멤버를 기반으로 합니다.

Equals(Object)

지정된 개체가 현재 개체와 같은지 확인합니다.

(다음에서 상속됨 Object)
GetEnumerator()

StateManagedCollection 컬렉션을 반복하는 반복기를 반환합니다.

GetHashCode()

기본 해시 함수로 작동합니다.

(다음에서 상속됨 Object)
GetKnownTypes()

파생 클래스에서 재정의된 경우 StateManagedCollection 컬렉션에 포함할 수 있는 IStateManager 형식의 배열을 가져옵니다.

GetType()

현재 인스턴스의 Type을 가져옵니다.

(다음에서 상속됨 Object)
MemberwiseClone()

현재 Object의 단순 복사본을 만듭니다.

(다음에서 상속됨 Object)
OnClear()

파생 클래스에서 재정의된 경우 Clear() 메서드가 컬렉션에서 항목을 모두 제거하기 전에 추가 작업을 수행합니다.

OnClearComplete()

파생 클래스에서 재정의된 경우 Clear() 메서드가 컬렉션에서 항목을 모두 제거한 후 추가 작업을 수행합니다.

OnInsert(Int32, Object)

파생 클래스에서 재정의된 경우 IList.Insert(Int32, Object) 또는 IList.Add(Object) 메서드가 컬렉션에 항목을 추가하기 전에 추가 작업을 수행합니다.

OnInsertComplete(Int32, Object)

파생 클래스에서 재정의된 경우 IList.Insert(Int32, Object) 또는 IList.Add(Object) 메서드가 컬렉션에 항목을 추가한 후 추가 작업을 수행합니다.

OnRemove(Int32, Object)

파생 클래스에서 재정의된 경우 IList.Remove(Object) 또는 IList.RemoveAt(Int32) 메서드가 컬렉션에서 지정한 항목을 제거하기 전에 추가 작업을 수행합니다.

OnRemoveComplete(Int32, Object)

파생 클래스에서 재정의된 경우 IList.Remove(Object) 또는 IList.RemoveAt(Int32) 메서드가 컬렉션에서 지정한 항목을 제거한 후에 추가 작업을 수행합니다.

OnValidate(Object)

파생 클래스에서 재정의된 경우 StateManagedCollection 컬렉션 요소의 유효성을 검사합니다.

SetDirty()

전체 StateManagedCollection 컬렉션이 뷰 상태에 직렬화되도록 합니다.

SetDirtyObject(Object)

파생 클래스에서 재정의된 경우 변경 정보만 기록하는 대신 전체 상태를 뷰 상태에 기록하도록 컬렉션에 포함된 object에 지시합니다.

ToString()

현재 개체를 나타내는 문자열을 반환합니다.

(다음에서 상속됨 Object)

명시적 인터페이스 구현

ICollection.Count

StateManagedCollection 컬렉션에 포함된 요소의 개수를 가져옵니다.

ICollection.IsSynchronized

StateManagedCollection 컬렉션이 동기화되어 스레드로부터 안전하게 보호되는지 여부를 나타내는 값을 가져옵니다. 이 메서드는 항상 false를 반환합니다.

ICollection.SyncRoot

StateManagedCollection 컬렉션에 대한 액세스를 동기화하는 데 사용할 수 있는 개체를 가져옵니다. 이 메서드는 항상 null를 반환합니다.

IEnumerable.GetEnumerator()

StateManagedCollection 컬렉션을 반복하는 반복기를 반환합니다.

IList.Add(Object)

StateManagedCollection 컬렉션에 항목을 추가합니다.

IList.Clear()

StateManagedCollection 컬렉션에서 모든 항목을 제거합니다.

IList.Contains(Object)

StateManagedCollection 컬렉션에 특정 값이 있는지 여부를 확인합니다.

IList.IndexOf(Object)

StateManagedCollection 컬렉션에서 지정한 항목의 인덱스를 확인합니다.

IList.Insert(Int32, Object)

항목을 StateManagedCollection 컬렉션 내의 지정한 인덱스에 삽입합니다.

IList.IsFixedSize

StateManagedCollection 컬렉션의 크기가 고정되어 있는지 여부를 나타내는 값을 가져옵니다. 이 메서드는 항상 false를 반환합니다.

IList.IsReadOnly

StateManagedCollection 컬렉션이 읽기 전용인지 여부를 나타내는 값을 가져옵니다.

IList.Item[Int32]

지정된 인덱스에 있는 IStateManager 요소를 가져옵니다.

IList.Remove(Object)

StateManagedCollection 컬렉션에서 맨 처음 발견되는 지정된 개체를 제거합니다.

IList.RemoveAt(Int32)

지정한 인덱스에 있는 IStateManager 요소를 제거합니다.

IStateManager.IsTrackingViewState

StateManagedCollection 컬렉션에서 해당 뷰 상태의 변경 내용을 저장하는지 여부를 나타내는 값을 가져옵니다.

IStateManager.LoadViewState(Object)

이전에 저장된 StateManagedCollection 컬렉션과 이 컬렉션에 포함된 IStateManager 항목의 뷰 상태를 복원합니다.

IStateManager.SaveViewState()

페이지가 서버에 포스트백된 이후에 발생한 StateManagedCollection 컬렉션과 이 컬렉션에 포함된 각 IStateManager 개체의 변경 내용을 저장합니다.

IStateManager.TrackViewState()

StateManagedCollection 컬렉션과 이 컬렉션에 포함된 각 IStateManager 개체가 해당 뷰 상태의 변경 내용을 추적하여 같은 페이지에서 발생하는 여러 요청에 대해 유지할 수 있도록 합니다.

확장 메서드

Cast<TResult>(IEnumerable)

IEnumerable의 요소를 지정된 형식으로 캐스팅합니다.

OfType<TResult>(IEnumerable)

지정된 형식에 따라 IEnumerable의 요소를 필터링합니다.

AsParallel(IEnumerable)

쿼리를 병렬화할 수 있도록 합니다.

AsQueryable(IEnumerable)

IEnumerableIQueryable로 변환합니다.

적용 대상

추가 정보