CA1006: Nie zagnieżdżaj typy rodzajowe w Członkowskim podpisów
TypeName |
DoNotNestGenericTypesInMemberSignatures |
CheckId |
CA1006 |
Kategoria |
Microsoft.Design |
Złamanie zmiany |
Złamanie |
Przyczyna
Widoczne z zewnątrz członek ma podpis, który zawiera argument typu zagnieżdżonego.
Opis reguły
Argument typu zagnieżdżonego jest argument typu, który jest również typu rodzajowego.Aby wywołać członek, którego podpis zawiera argument typu zagnieżdżonego, użytkownik musi wystąpienia jednego typu rodzajowego i przekazywania tego typu do konstruktora drugi typ rodzajowy.Wymagane procedury i składni są złożone i należy unikać.
Jak naprawić naruszenia
Aby usunąć naruszenie tej zasady, należy zmienić projekt, aby usunąć argument typu zagnieżdżonego.
Kiedy do pomijania ostrzeżenia
Nie pomijaj ostrzeżenie od tej reguły.Dostarczanie produktów pierwotnych w składni, która jest łatwe do zrozumienia i użyć zmniejsza czas, który jest wymagany, aby dowiedzieć się i zwiększa szybkość przyjęcia nowych bibliotek.
Przykład
W poniższym przykładzie metody, które powodują naruszenie reguł i składni, który jest niezbędny do wywoływania metody naruszyli.
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);
}
}
}
Zasady pokrewne
CA1005: Uniknąć nadmiernego parametrów na typy rodzajowe
CA1010: Kolekcji należy implementować interfejs rodzajowy
CA1000: Elementy statyczne nie są deklarowane w typach podstawowych
CA1002: Nie będą uwidaczniać rodzajowy list
CA1004: Metody rodzajowe powinny dostarczyć parametr typu
CA1003: Użyj wystąpień programu obsługi zdarzeń rodzajowy
CA1007: Użyj generyczne, gdzie stosowne