共用方式為


CA1038:列舉程式應該是強類型

型別名稱

EnumeratorsShouldBeStronglyTyped

CheckId

CA1038

分類

Microsoft.Design

中斷變更

中斷

原因

公用或保護的型別會實作 IEnumerator,但不提供 IEnumerator.Current 屬性的強型別 (Strongly Typed) 版本。衍生自下列型別的型別不受限於此規則:

規則描述

此規則需要 IEnumerator 實作才能同時提供 Current 屬性的強型別版本,如此當使用者使用介面提供的功能時,就不需要將傳回值轉換為強型別。此規則會假設實作 IEnumerator 的型別包含型別的執行個體 (Instance) 集合,且該型別強過 Object

如何修正違規

若要修正此規則的違規情形,請明確實作介面屬性 (將該屬性宣告為 IEnumerator.Current)。加入宣告為 Current 的屬性公用強型別版本,並傳回強型別物件。

隱藏警告的時機

在實作物件架構列舉值以便與物件架構集合搭配使用時 (例如二進位樹狀目錄),請排除此規則的警告。擴充新集合的型別將定義強型別列舉值,並公開 (Expose) 強型別屬性。

範例

下列範例會實作強型別 IEnumerator 型別的正確方法。

using System;
using System.Collections;
namespace DesignLibrary
{
   // The ExceptionEnumerator class implements a strongly typed enumerator  
   // for the ExceptionCollection type. 

   public class ExceptionEnumerator: IEnumerator
   {
      private IEnumerator myCollectionEnumerator;

      private ExceptionEnumerator () {}

      public ExceptionEnumerator(ExceptionCollection collection)
      {
         myCollectionEnumerator = collection.data.GetEnumerator();
      }

      // Implement the IEnumerator interface member explicitly. 
      object IEnumerator.Current
      {
         get 
         {
            return myCollectionEnumerator.Current;
         }
      }

      // Implement the strongly typed member. 
      public Exception Current
      {
         get 
         {
            return (Exception) myCollectionEnumerator.Current;
         }
      }

      // Implement the remaining IEnumerator members. 
      public bool MoveNext ()
      {
         return myCollectionEnumerator.MoveNext();
      }

      public void Reset ()
      {
         myCollectionEnumerator.Reset();
      }
   }

   public class ExceptionCollection : ICollection
   {   
      internal ArrayList data;

      ExceptionCollection()
      {
         data = new ArrayList();
      }

      // Provide the explicit interface member for ICollection. 
      void ICollection.CopyTo(Array array, int index)
      {
         data.CopyTo(array, index);
      }

      // Provide the strongly typed member for ICollection. 
      public void CopyTo(Exception[] array, int index)
      {
         ((ICollection)this).CopyTo(array, index);
      }

      // Implement the rest of the ICollection members. 
      public int Count
      {
        get 
        {
           return data.Count;
        }
      }

      public object SyncRoot
      {
         get 
        {
           return this; 
        }
      }

      public bool IsSynchronized
      {
         get 
         {
            return false; 
         }
      }

      // The IEnumerable interface is implemented by ICollection.
      IEnumerator IEnumerable.GetEnumerator()
      {
         return new ExceptionEnumerator(this);
      }

      public ExceptionEnumerator GetEnumerator()
      {
         return new ExceptionEnumerator(this);
      }
   }
}

相關規則

CA1035:ICollection 實作包含強類型成員

CA1039:清單為強類型

請參閱

參考

IEnumerator

CollectionBase

DictionaryBase

ReadOnlyCollectionBase