StateManagedCollection.CreateKnownType(Int32) メソッド

定義

派生クラスでオーバーライドされた場合は、 IStateManagerを実装するクラスのインスタンスを作成します。 作成されるオブジェクトの型は、 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

パラメーター

index
Int32

作成するIStateManagerの型の、GetKnownTypes()によって返される型の順序付きリストからのインデックス。

返品

指定されたindexに従って、IStateManagerから派生したクラスのインスタンス。

例外

いずれの場合も、派生クラスでオーバーライドされない場合。

次のコード例は、厳密に型指定された StateManagedCollection クラスが CreateKnownType メソッドを実装する方法を示しています。 CreateKnownTypeCycleCollection実装では、渡されたインデックスに応じて、Bicycle または Tricycle オブジェクトの既定のインスタンスが返されます。 このコード例は、 StateManagedCollection クラスに提供されるより大きな例の一部です。

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

注釈

CreateKnownType メソッドは、StateManagedCollection.IStateManager.LoadViewState メソッドの実装でStateManagedCollection コレクションによって内部的に呼び出されます。 派生コレクションは、CreateKnownType メソッドをオーバーライドして、指定されたindexによって識別されるIStateManager型の既定のインスタンスを返します。これは、GetKnownTypes メソッドによって返される型のいずれかにマップされます。

適用対象

こちらもご覧ください