مشاركة عبر


CA1006: Do not nest generic types in member signatures

TypeName

DoNotNestGenericTypesInMemberSignatures

CheckId

ca1006

Category

Microsoft.تصميم

تعطيل تغيير

فصل

السبب

عضو مرئي خارجياً توقيع يحتوي على وسيطة نوع متداخل.

وصف القاعدة

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

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

إلى إصلاحه انتهاكا لهذه قاعدة، قم بتغيير التصميم إلى إزالة وسيطة نوع متداخل.

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

لا بمنع تحذير من هذه قاعدة. توفير عام في بناء جملة هو سهلة الفهم واستخدام تقليل الوقت الذي هو لمعرفة وزيادة معدل تبني مكتبات جديدة.

مثال

يوضح المثال التالي طريقة تخالف قاعدة وبناء الجملة المطلوب إلى استدعاء الأسلوب violating.

Imports System
Imports System.Collections.Generic

Namespace DesignLibrary

   Public Class IntegerCollections

      Sub NonNestedCollection(collection As ICollection(Of Integer))

         For Each I As Integer In DirectCast( _ 
            collection, IEnumerable(Of Integer))

            Console.WriteLine(I)

         Next 

      End Sub

      ' This method violates the rule.
      Sub NestedCollection( _ 
         outerCollection As ICollection(Of ICollection(Of Integer)))

         For Each innerCollection As ICollection(Of Integer) In _ 
            DirectCast(outerCollection, _ 
                       IEnumerable(Of ICollection(Of Integer)))

            For Each I As Integer In _ 
               DirectCast(innerCollection, IEnumerable(Of Integer))

               Console.WriteLine(I)

            Next

         Next

      End Sub

   End Class

   Class Test

      Shared Sub Main()

         Dim collections As New IntegerCollections()

         Dim integerListA As New List(Of Integer)()
         integerListA.Add(1)
         integerListA.Add(2)
         integerListA.Add(3)

         collections.NonNestedCollection(integerListA)

         Dim integerListB As New List(Of Integer)()
         integerListB.Add(4)
         integerListB.Add(5)
         integerListB.Add(6)

         Dim integerListC As New List(Of Integer)()
         integerListC.Add(7)
         integerListC.Add(8)
         integerListC.Add(9)

         Dim nestedIntegerLists As New List(Of ICollection(Of Integer))()
         nestedIntegerLists.Add(integerListA)
         nestedIntegerLists.Add(integerListB)
         nestedIntegerLists.Add(integerListC)

         collections.NestedCollection(nestedIntegerLists)

      End Sub

   End Class

End Namespace
using System;
using System.Collections.Generic;

namespace DesignLibrary
{
   public class IntegerCollections
   {
      public void NotNestedCollection(ICollection<int> collection)
      {
         foreach(int i in collection)
         {
            Console.WriteLine(i);
         }
      }

      // This method violates the rule.
      public void NestedCollection(
         ICollection<ICollection<int>> outerCollection)
      {
         foreach(ICollection<int> innerCollection in outerCollection)
         {
            foreach(int i in innerCollection)
            {
               Console.WriteLine(i);
            }
         }
      }
   }

   class Test
   {
      static void Main()
      {
         IntegerCollections collections = new IntegerCollections();

         List<int> integerListA = new List<int>();
         integerListA.Add(1);
         integerListA.Add(2);
         integerListA.Add(3);

         collections.NotNestedCollection(integerListA);

         List<int> integerListB = new List<int>();
         integerListB.Add(4);
         integerListB.Add(5);
         integerListB.Add(6);

         List<int> integerListC = new List<int>();
         integerListC.Add(7);
         integerListC.Add(8);
         integerListC.Add(9);

         List<ICollection<int>> nestedIntegerLists = 
            new List<ICollection<int>>();
         nestedIntegerLists.Add(integerListA);
         nestedIntegerLists.Add(integerListB);
         nestedIntegerLists.Add(integerListC);

         collections.NestedCollection(nestedIntegerLists);
      }
   }
}

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

ca1005: تجنب الإفراط معلمات تشغيل الأنواع العامة

ca1303: لا تمرير القيم الحرفية كمترجم معلمات

ca1000: لا بتعريف الأعضاء ثابتة تشغيل الأنواع العامة

ca1002: لا تعرض القوائم العامة

ca1004: يجب توفير وظائف عامة معلمة نوع

ca1003: استخدام مثيلات معالج حدث العام

CA1007: Use generics where appropriate

راجع أيضًا:

المرجع

generics (C# البرمجة الدليل)