StateManagedCollection.CreateKnownType(Int32) 方法     
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中替代时,创建实现 IStateManager 的类的实例。 所创建对象的类型基于由 GetKnownTypes() 方法返回的集合的指定成员。
protected:
 virtual System::Object ^ CreateKnownType(int index);protected virtual object CreateKnownType (int index);abstract member CreateKnownType : int -> obj
override this.CreateKnownType : int -> objProtected Overridable Function CreateKnownType (index As Integer) As Object参数
- index
- Int32
要创建的 IStateManager 的类型的索引(来自 GetKnownTypes() 返回的有序类型列表)。
返回
根据提供的 index 从 IStateManager 派生的类的实例。
例外
用于不在派生的类中重写的所有情况。
示例
下面的代码示例演示强类型 StateManagedCollection 类如何实现 CreateKnownType 该方法。 返回CycleCollection或对象的默认实例Bicycle``Tricycle的实现CreateKnownType,具体取决于传递的索引。 此代码示例是为类提供的大型示例的 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的类型之一。