CA1038 : Les énumérateurs doivent être fortement typés

Élément Valeur
ID de la règle CA1038
Category Microsoft.Design
Modification avec rupture Rupture

Cause

Un type public ou protégé implémente System.Collections.IEnumerator, mais ne fournit pas de version fortement typée de la propriété System.Collections.IEnumerator.Current. Les types dérivés des types suivants sont exemptés de cette règle :

Notes

Cette règle est déconseillée. Pour plus d’informations, consultez Règles dépréciées.

Description de la règle

Cette règle exige que les implémentations IEnumerator fournissent également une version fortement typée de la propriété Current pour que les utilisateurs ne soient pas tenus d’effectuer un cast de la valeur de retour en type fort quand ils utilisent les fonctionnalités fournies par l’interface. Cette règle suppose que le type qui implémente IEnumerator contient une collection d’instances d’un type plus fort que Object.

Comment corriger les violations

Pour corriger une violation de cette règle, implémentez explicitement la propriété d’interface (déclarez-la comme IEnumerator.Current). Ajoutez une version publique fortement typée de la propriété, déclarée comme Current, et faites en sorte qu’elle retourne un objet fortement typé.

Quand supprimer les avertissements

Supprimez un avertissement de cette règle quand vous implémentez un énumérateur basé sur des objets à utiliser avec une collection basée sur des objets comme un arbre binaire. Les types qui étendent la nouvelle collection définissent l’énumérateur fortement typé et exposent la propriété fortement typée.

Exemple

L’exemple suivant illustre la façon correcte d’implémenter un type IEnumerator fortement typé.

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 : Les implémentations ICollection possèdent des membres fortement typés

CA1039 : Les listes sont fortement typées

Voir aussi