다음을 통해 공유


멤버 시그니처에 제네릭 형식을 중첩하지 마십시오.

업데이트: 2007년 11월

TypeName

DoNotNestGenericTypesInMemberSignatures

CheckId

CA1006

범주

Microsoft.Design

변경 수준

주요 변경

원인

외부에서 볼 수 있는 멤버에 중첩된 형식 인수가 포함된 시그너처가 있습니다.

규칙 설명

중첩된 형식 인수는 제네릭 형식의 인수입니다. 시그너처에 중첩된 형식 인수가 포함되어 있는 멤버를 호출하려면 제네릭 형식 하나를 인스턴스화하고 이 형식을 두 번째 제네릭 형식의 생성자에 전달해야 합니다. 이 경우 복잡한 프로시저와 구문이 필요하므로 이 방법을 피해야 합니다.

위반 문제를 해결하는 방법

이 규칙 위반 문제를 해결하려면 디자인을 변경하여 중첩된 형식 인수를 제거합니다.

경고를 표시하지 않는 경우

이 규칙에서는 경고를 표시해야 합니다. 제네릭을 이해하고 사용하기 쉬운 구문으로 제공하면 학습에 걸리는 시간이 줄어들고 더 많은 사용자가 새로운 라이브러리를 선택하게 됩니다.

예제

다음 예제에서는 규칙을 위반하는 메서드와 위반 메서드를 호출하는 데 필요한 구문을 보여 줍니다.

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

관련 규칙

제네릭 형식에 매개 변수를 너무 많이 사용하지 마십시오.

컬렉션은 제네릭 인터페이스를 구현해야 합니다.

정적 멤버를 제네릭 형식으로 선언하지 마십시오.

제네릭 목록을 노출하지 마십시오.

제네릭 메서드는 형식 매개 변수를 제공해야 합니다.

제네릭 이벤트 처리기 인스턴스를 사용하십시오.

적합한 제네릭을 사용하십시오.

참고 항목

참조

제네릭(C# 프로그래밍 가이드)