Condividi tramite


CA1006: Non annidare tipi generici nelle firme dei membri

TypeName

DoNotNestGenericTypesInMemberSignatures

CheckId

CA1006

Category

Microsoft.Design

Breaking Change

Breaking

Causa

Un membro visibile esternamente presenta una firma che contiene un argomento di tipo annidato.

Descrizione della regola

Un argomento di tipo annidato è un argomento di tipo che è anche un tipo generico.Per chiamare un membro la cui firma contiene un argomento di tipo annidato, l'utente deve creare un'istanza su un tipo generico e passare il tipo al costruttore di un secondo tipo generico.La sintassi e la procedura necessarie sono complesse ed è opportuno evitarle.

Come correggere le violazioni

Per correggere una violazione di questa regola, modificare la progettazione per rimuovere l'argomento di tipo annidato.

Esclusione di avvisi

Non escludere un avviso da questa regola.La presenza di generics in una sintassi facile da comprendere e utilizzare riduce il tempo necessario all'apprendimento e aumenta la frequenza di adozione di nuove librerie.

Esempio

Nell'esempio riportato di seguito sono illustrati un metodo che viola la regola e la sintassi necessaria per chiamare tale metodo.

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);
      }
   }
}

Regole correlate

CA1005: Evitare un uso eccessivo di parametri nei tipi generici

CA1010: Le raccolte devono implementare un'interfaccia generica

CA1000: Non dichiarare membri statici su tipi generici

CA1002: Non esporre elenchi generici

CA1004: I metodi generici devono fornire parametri di tipo

Ca1003: Utilizzare istanze di gestori eventi generici

CA1007: Utilizzare generics dove appropriato

Vedere anche

Riferimenti

Generics (Guida per programmatori C#)