CA1006: 멤버 시그니처에 제네릭 형식을 중첩하지 마십시오.
항목 | 값 |
---|---|
RuleId | CA1006 |
범주 | Microsoft.Design |
주요 변경 내용 | 주요 변경 |
원인
외부에 표시되는 멤버에 중첩 형식 인수를 포함하는 시그니처가 있습니다.
규칙 설명
중첩된 형식 인수는 제네릭 형식의 인수입니다. 시그니처에 중첩된 형식 인수가 포함되어 있는 멤버를 호출하려면 제네릭 형식 하나를 인스턴스화하고 이 형식을 두 번째 제네릭 형식의 생성자에 전달해야 합니다. 이 경우 복잡한 프로시저와 구문이 필요하므로 이 방법을 피해야 합니다.
위반 문제를 해결하는 방법
이 규칙의 위반 문제를 해결하려면 중첩 형식 인수를 제거하도록 디자인을 변경합니다.
경고를 표시하지 않는 경우
이 규칙에서는 경고를 표시해야 합니다. 쉽게 이해하고 사용할 수 있는 구문에서 제네릭을 제공하면 새 라이브러리 도입률을 배우고 늘리는 데 필요한 시간이 단축됩니다.
예시
다음 예제에서는 규칙을 위반하는 메서드와 위반 메서드를 호출하는 데 필요한 구문을 보여 줍니다.
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: 제네릭 형식에 매개 변수를 너무 많이 사용하지 마십시오.
CA1010: 컬렉션은 제네릭 인터페이스를 구현해야 합니다.
CA1000: 정적 멤버를 제네릭 형식으로 선언하지 마십시오.
CA1004: 제네릭 메서드는 형식 매개 변수를 제공해야 합니다.
CA1003: 제네릭 이벤트 처리기 인스턴스를 사용하십시오.
참고 항목
피드백
https://aka.ms/ContentUserFeedback
출시 예정: 2024년 내내 콘텐츠에 대한 피드백 메커니즘으로 GitHub 문제를 단계적으로 폐지하고 이를 새로운 피드백 시스템으로 바꿀 예정입니다. 자세한 내용은 다음을 참조하세요.다음에 대한 사용자 의견 제출 및 보기