StateManagedCollection.CreateKnownType(Int32) 方法

定义

在派生类中替代时,创建实现 IStateManager 的类的实例。When overridden in a derived class, creates an instance of a class that implements IStateManager. 所创建对象的类型基于由 GetKnownTypes() 方法返回的集合的指定成员。The type of object created is based on the specified member of the collection returned by the GetKnownTypes() method.

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() 返回的有序类型列表)。The index, from the ordered list of types returned by GetKnownTypes(), of the type of IStateManager to create.

返回

Object

根据提供的 indexIStateManager 派生的类的实例。An instance of a class derived from IStateManager, according to the index provided.

例外

用于不在派生的类中重写的所有情况。In all cases when not overridden in a derived class.

示例

下面的代码示例演示强类型类如何 StateManagedCollection 实现 CreateKnownType 方法。The following code example demonstrates how a strongly typed StateManagedCollection class implements the CreateKnownType method. CycleCollection的实现根据 CreateKnownType Bicycle 传递的索引返回或对象的默认实例 TricycleThe CycleCollection implementation of CreateKnownType returns the default instance of either a Bicycle or Tricycle object, depending on the index passed. 此代码示例是为类提供的更大示例的一部分 StateManagedCollectionThis code example is part of a larger example provided for the StateManagedCollection class.

//////////////////////////////////////////////////////////////
//
// 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 在方法的实现中由集合在内部调用 StateManagedCollection.IStateManager.LoadViewStateThe CreateKnownType method is called internally by the StateManagedCollection collection in its implementation of the StateManagedCollection.IStateManager.LoadViewState method. 派生集合重写 CreateKnownType 方法以返回 IStateManager 由提供的标识的类型的默认实例 index ,该实例映射到该方法返回的类型之一 GetKnownTypesDerived collections override the CreateKnownType method to return a default instance of the IStateManager type identified by the provided index, which maps to one of the types returned by the GetKnownTypes method.

适用于

另请参阅