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 方法。 的實作 GetKnownTypes 會 CycleCollection
傳 Type 回已知型別的陣列,包括 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
備註
方法 GetKnownTypes 在方法的 IStateManager.SaveViewState 實作中,由 集合在內部 StateManagedCollection 呼叫。 衍生集合會覆寫 方法, GetKnownTypes 以傳回物件陣列 Type ,這些物件代表集合可以包含的類型。