다음을 통해 공유


CA1038: 열거자는 강력한 형식이어야 합니다.

TypeName

EnumeratorsShouldBeStronglyTyped

CheckId

CA1038

범주

Microsoft.Design

변경 수준

주요 변경

원인

public 또는 protected 형식이 System.Collections.IEnumerator를 구현하지만 IEnumerator.Current 속성의 강력한 형식 버전을 제공하지 않습니다. 다음 형식에서 파생된 형식은 이 규칙에서 예외입니다.

규칙 설명

이 규칙에서는 IEnumerator 구현에서 Current 속성의 강력한 형식 버전도 제공하도록 하여 사용자가 인터페이스에서 제공하는 기능을 사용할 때 반환 값을 강력한 형식으로 캐스팅할 필요가 없도록 합니다. 이 규칙에서는 IEnumerator를 구현하는 형식에 Object보다 강력한 형식의 인스턴스 컬렉션이 들어 있다고 가정합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 인터페이스 속성을 IEnumerator.Current로 선언하여 명시적으로 구현합니다. Current로 선언된, 속성에 대한 강력한 형식의 public 버전을 추가하고 강력한 형식의 개체를 반환하도록 합니다.

경고를 표시하지 않는 경우

이진 트리와 같은 개체 기반 컬렉션에서 사용할 개체 기반 열거자를 구현할 때는 이 규칙에 따른 경로를 표시하지 마십시오. 새 컬렉션을 확장하는 형식은 강력한 형식의 열거자를 정의하고 강력한 형식의 속성을 노출합니다.

예제

다음 예제에서는 강력한 형식의 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: 목록은 강력한 형식이어야 합니다.

참고 항목

참조

System.Collections.IEnumerator

System.Collections.CollectionBase

System.Collections.DictionaryBase

System.Collections.ReadOnlyCollectionBase