Compartir a través de


CA1038: Los enumeradores deben estar fuertemente tipados

Nombre de tipo

EnumeratorsShouldBeStronglyTyped

Identificador de comprobación

CA1038

Categoría

Microsoft.Design

Cambio problemático

Motivo

Un tipo público o protegido implementa System.Collections.IEnumerator pero no proporciona una versión fuertemente tipada de la propiedad IEnumerator.Current.Los tipos que se derivan de los tipos siguientes están exentos de esta regla:

Descripción de la regla

Esta regla requiere implementaciones de IEnumerator para proporcionar una versión fuertemente tipada de la propiedad Current para que los usuarios no tengan que convertir el valor devuelto en un tipo inflexible cuando utilicen la funcionalidad proporcionada por la interfaz.Esta regla supone que el tipo que implementa IEnumerator contiene una colección de instancias de un tipo de mayor prioridad que Object.

Cómo corregir infracciones

Para corregir una infracción de esta regla, implemente explícitamente la propiedad de la interfaz (declárela como IEnumerator.Current).Agregue una versión pública de la propiedad fuertemente tipada, declarada como Current, y haga que se devuelva un objeto fuertemente tipado.

Cuándo suprimir advertencias

Suprima una advertencia de esta regla cuando implemente un enumerador basado en objetos para utilizarlo con una colección basada en objetos, como un árbol binario.Los tipos que extienden la nueva colección definirán el enumerador fuertemente tipado y expondrán la propiedad fuertemente tipada.

Ejemplo

El ejemplo siguiente muestra la manera correcta de implementar un tipo IEnumerator fuertemente tipado.

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);
      }
   }
}

Reglas relacionadas

CA1035: Las implementaciones de ICollection tienen miembros fuertemente tipados

CA1039: Las listas están fuertemente tipadas

Vea también

Referencia

System.Collections.IEnumerator

System.Collections.CollectionBase

System.Collections.DictionaryBase

System.Collections.ReadOnlyCollectionBase