CA1038: Enumerators should be strongly typed

TypeName

EnumeratorsShouldBeStronglyTyped

CheckId

ca1038

Category

Microsoft.تصميم

تعطيل تغيير

فصل

السبب

نوع عامة أو محمية بتنفيذ System.Collections.IEnumeratorولكنها لا توفر الإصدار مطبوع بشدة IEnumerator.Currentخاصية. الأنواع المشتقة من الأنواع التالية المستثناة من هذه قاعدة:

وصف القاعدة

هذه قاعدة يتطلب IEnumeratorتطبيقات إلى أيضا بتوفير الإصدار مطبوع بشدة Currentخاصية حيث يمكن للمستخدمين غير مطلوبة إلى تحويل قيمة الإرجاع إلى نوع قوي عند استخدامهم للوظائف المتوفرة من قبل الواجهة. تفترض هذه قاعدة التي نوع تطبيق IEnumeratorيحتوي على مجموعة من الحالات التي تكون أكثر قوة من نوع Object.

كيف إلى الإصلاح انتهاكات

إلى إصلاح انتهاكا لهذه قاعدة، قم بتطبيق خاصية واجهة بوضوح (التصريح ك IEnumerator.Current). إضافة نسخة مكتوبة بشدة عام خاصية، تعريف $ $ $ $ كـ Current، ويكون ذلك بإرجاع كائن مكتوب بشدة.

عند إلى منع التحذيرات

منع ظهور تحذير من هذه قاعدة عند تطبيق يستند إلى كائن العداد للاستخدام مع مجموعة تستند إلى الكائنات، مثل شجرة ثنائية. يتم تعريف أنواع توسيع المجموعة الجديدة العداد مكتوب بشدة و كشف خاصية مكتوب بشدة.

مثال

يوضح المثال التالي طريقة الصحيح إلى تطبيق مكتوب بشدة 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);
      }
   }
}

القواعد ذات الصلة

Instrument a Statically Compiled ASP.NET Web Application and Collect Memory Data by Using the Profiler Command Line Instrument a Statically Compiled ASP.NET Application and Collect Memory Data

ca1039: قوائم تمت كتابتها بشدة

راجع أيضًا:

المرجع

System.Collections.IEnumerator

System.Collections.CollectionBase

System.Collections.DictionaryBase

System.Collections.ReadOnlyCollectionBase