CA1006: No anidar tipos genéricos en signaturas de miembro

Elemento Valor
RuleId CA1006
Category Microsoft.Design
Cambio importante Problemático

Causa

Un miembro visible externamente tiene una firma que contiene un argumento de tipo anidado.

Descripción de la regla

Un argumento de tipo anidado es un argumento de tipo que también es un tipo genérico. Para llamar a un miembro cuya firma contenga un argumento de tipo anidado, el usuario debe crear instancias de un tipo genérico y pasar este tipo al constructor de un segundo tipo genérico. El procedimiento y la sintaxis necesarios para ello son complejos, por lo que es preferible evitarlo.

Cómo corregir infracciones

Para corregir una infracción de esta regla, cambie el diseño para quitar el argumento de tipo anidado.

Cuándo suprimir las advertencias

No suprima las advertencias de esta regla. Proporcionar tipos genéricos en una sintaxis fácil de entender y usar reduce el tiempo necesario para aprender y aumentar la tasa de adopción de nuevas bibliotecas.

Ejemplo

En el ejemplo siguiente se muestra un método que infringe la regla y la sintaxis necesaria para llamar al método infractor.

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: Evitar los parámetros excesivos en tipos genéricos

CA1010: Las colecciones deben implementar la interfaz genérica

CA1000: No declarar miembros estáticos en tipos genéricos

CA1002: No exponer listas genéricas

CA1004: Los métodos genéricos deben proporcionar un parámetro de tipo

CA1003: Utilizar instancias genéricas de controlador de eventos

CA1007: Utilizar valores genéricos cuando sea posible

Consulte también

Genéricos