StateManagedCollection.CreateKnownType(Int32) Método

Definição

Quando sobrescrito numa classe derivada, cria-se uma instância de uma classe que implementa IStateManager. O tipo de objeto criado baseia-se no membro especificado da coleção devolvido pelo GetKnownTypes() método.

protected:
 virtual System::Object ^ CreateKnownType(int index);
protected virtual object CreateKnownType(int index);
abstract member CreateKnownType : int -> obj
override this.CreateKnownType : int -> obj
Protected Overridable Function CreateKnownType (index As Integer) As Object

Parâmetros

index
Int32

O índice, da lista ordenada de tipos devolvida por GetKnownTypes(), do tipo de IStateManager para criar.

Devoluções

Uma instância de uma classe derivada de IStateManager, de acordo com o index fornecido.

Exceções

Em todos os casos em que não for sobreposto numa classe derivada.

Exemplos

O exemplo de código seguinte demonstra como uma classe fortemente tipada StateManagedCollection implementa o CreateKnownType método. A CycleCollection implementação de CreateKnownType devolve a instância padrão de um Bicycle objeto ou Tricycle ou, dependendo do índice passado. Este exemplo de código faz parte de um exemplo maior fornecido para a StateManagedCollection classe.

//////////////////////////////////////////////////////////////
//
// 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 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

Observações

O CreateKnownType método é chamado internamente pela StateManagedCollection coleção na sua implementação do StateManagedCollection.IStateManager.LoadViewState método. As coleções derivadas sobrepõem o CreateKnownType método para devolver uma instância padrão do IStateManager tipo identificado pelo fornecido index, que corresponde a um dos tipos devolvidos pelo GetKnownTypes método.

Aplica-se a

Ver também