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

索引,來自 GetKnownTypes() 所傳回的已排序類型清單,並屬於所要建立之 IStateManager 的類型。

傳回

Object

依據所提供的 IStateManager,從 index 衍生之類別的執行個體。

例外狀況

未在衍生的類別中覆寫時的所有情況。

範例

下列程式碼範例示範強型別 StateManagedCollection 類別如何實作 CreateKnownType 方法。 的 CycleCollection 實作 CreateKnownType 會根據傳遞的 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 傳回的其中一個型別。

適用於

另請參閱