StateManagedCollection.GetKnownTypes 方法
定义
重要
一些信息与预发行产品相关,相应产品在发行之前可能会进行重大修改。 对于此处提供的信息,Microsoft 不作任何明示或暗示的担保。
在派生类中替代时,获取 StateManagedCollection 集合可以包含的 IStateManager 类型的数组。
protected:
virtual cli::array <Type ^> ^ GetKnownTypes();
protected virtual Type[] GetKnownTypes ();
abstract member GetKnownTypes : unit -> Type[]
override this.GetKnownTypes : unit -> Type[]
Protected Overridable Function GetKnownTypes () As Type()
返回
- Type[]
Type 对象的有序数组,用于标识集合可以包含的 IStateManager 对象的类型。 默认实现返回 null
。
示例
下面的代码示例演示强类型 StateManagedCollection 类如何实现 GetKnownTypes 该方法。 返回CycleCollection
已知Type类型的数组(包括Bicycle
和Tricycle
类型)的实现GetKnownTypes。 此代码示例是为类提供的大型示例的 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
注解
该方法GetKnownTypes在该方法的IStateManager.SaveViewState实现中由StateManagedCollection集合在内部调用。 派生集合重写 GetKnownTypes 该方法,以返回表示集合可以包含的类型的对象数组 Type 。