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

항목
RuleId CA1038
범주 Microsoft.Design
주요 변경 내용 주요 변경

원인

public 또는 protected 형식이 System.Collections.IEnumerator를 구현하지만 System.Collections.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: 목록은 강력한 형식이어야 합니다.

참고 항목