StateManagedCollection.CreateKnownType(Int32) Metodo

Definizione

Quando sottoposto a override in una classe derivata, crea un'istanza di una classe che implementa IStateManager. Il tipo di oggetto creato è basato sul membro specificato della raccolta restituita dal metodo GetKnownTypes().

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

Parametri

index
Int32

Indice, dall'elenco ordinato di tipi restituito dal metodo GetKnownTypes(), del tipo di oggetto IStateManager da creare.

Restituisce

Object

Istanza di una classe derivata dall'oggetto IStateManager, in base al parametro index fornito.

Eccezioni

In tutti i casi quando non è sottoposto a override in una classe derivata.

Esempio

Nell'esempio di codice seguente viene illustrato come una classe fortemente tipizzata implementa StateManagedCollection il CreateKnownType metodo. L'implementazione CycleCollection di CreateKnownType restituisce l'istanza predefinita di un Bicycle oggetto o Tricycle , a seconda dell'indice passato. Questo esempio di codice fa parte di un esempio più grande fornito per la 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

Commenti

Il CreateKnownType metodo viene chiamato internamente dalla raccolta nell'implementazione StateManagedCollection StateManagedCollection.IStateManager.LoadViewState del metodo. Le raccolte derivate eseguono l'override del CreateKnownType metodo per restituire un'istanza predefinita del IStateManager tipo identificato dall'oggetto indexGetKnownTypes , che esegue il mapping a uno dei tipi restituiti dal metodo .

Si applica a

Vedi anche