Compartilhar via


Não se aninham tipos genéricos em assinaturas de membro

TypeName

DoNotNestGenericTypesInMemberSignatures

CheckId

CA1006

Category (Categoria)

Microsoft.Design

Quebrando alterar

Quebrando

Causa

Um membro visível externamente tem uma assinatura que contém um argumento de tipo aninhado.

Descrição da regra

Um argumento de tipo aninhado é um argumento de tipo que também é um tipo genérico.Para chamar um membro cuja assinatura contém um argumento de tipo aninhado, o usuário deve criar uma instância de um tipo genérico e passar esse tipo para o construtor de um segundo tipo genérico.O procedimento necessário e a sintaxe é complexas e deve ser evitada.

Como corrigir violações

Para corrigir uma violação dessa regra alterar o design para remover o argumento de tipo aninhado.

Quando suprimir avisos

Não suprimir um aviso da regra.Fornecer os genéricos em uma sintaxe que seja fácil de entender e usar reduz o time de it que é necessário para aprender e aumenta a taxa de adoção de novas bibliotecas.

Exemplo

O exemplo a seguir mostra um método que viola a regra e a sintaxe necessária para telefonar viola o método.

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

Regras relacionadas

Evitar o excesso parâmetros em tipos genéricos

Coleções devem implementar a interface genérica

Não declarará membros estático tipos genéricos

Não expõem listas genéricas

Métodos genéricos devem fornecer o parâmetro de tipo

Usar instâncias de manipulador do evento genérico

Use genéricos onde for apropriado

Consulte também

Referência

Generics (Guia de programação C#)