Compartilhar via


CA1038: Enumeradores devem ser fortemente tipados

Item Valor
RuleId CA1038
Categoria Microsoft.Design
Alteração da falha Quebra

Causa

Um tipo público ou protegido implementa System.Collections.IEnumerator, mas não fornece uma versão fortemente tipada da propriedade System.Collections.IEnumerator.Current. Os tipos derivados dos seguintes tipos são isentos dessa regra:

Observação

Esta regra foi preterida. Para obter mais informações, confira Regras preteridas.

Descrição da regra

Essa regra exige que implementações de IEnumerator também forneçam uma versão fortemente tipada da propriedade Current, de forma que usuários não sejam obrigados a converter o valor de retorno no tipo forte quando usarem a funcionalidade fornecida pela interface. Essa regra pressupõe que o tipo que implementa IEnumerator contém uma coleção de instâncias de um tipo mais forte que Object.

Como corrigir violações

Para corrigir uma violação dessa regra, implemente a propriedade de interface explicitamente (declare-a como IEnumerator.Current). Adicione uma versão pública fortemente tipada da propriedade, declarada como Current, e faça com que ela retorne um objeto fortemente tipado.

Quando suprimir avisos

Suprima um aviso dessa regra ao implementar um enumerador baseado em objeto para uso com uma coleção baseada em objeto, como uma árvore binária. Os tipos que estendem a nova coleção vão definir o enumerador fortemente tipado e expor a propriedade fortemente tipada.

Exemplo

O exemplo a seguir demonstra a maneira correta de implementar um tipo IEnumerator fortemente 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);
      }
   }
}

CA1035: Implementações ICollection têm membros fortemente tipados

CA1039: Listas são fortemente tipadas

Confira também