그래서 List
에 대한 몇 가지 기본 확장 메서드를 만들려고했습니다. 기본적으로 UniqueAdd 및 UniqueAddRange가 있습니다. 추가하기 전에 값의 존재 여부를 확인하고 목록에 이미 값이 추가되어 있는지 확인합니다. 여기에 코드입니다 :List 확장 메서드로 Microsoft.Maintainability 오류
public static class ListExtensions
{
/// <summary>
/// Adds only the values in the 'values' collection that do not already exist in the list. Uses list.Contains() to determine existence of
/// previous values.
/// </summary>
/// <param name="list"></param>
/// <param name="values"></param>
public static void UniqueAddRange<T>(this List<T> list, IEnumerable<T> values)
{
foreach (T value in values)
{
list.UniqueAdd(value);
}
}
/// <summary>
/// Adds the value to the list only if it does not already exist in the list. Uses list.Contains() to determine existence of previos values.
/// </summary>
/// <typeparam name="T"></typeparam>
/// <param name="list"></param>
/// <param name="value"></param>
public static void UniqueAdd<T>(this List<T> list, T value)
{
if (!list.Contains(value))
{
list.Add(value);
}
}
}
는 그리고 건물 때 다음과 같은 오류가 : 여기
CA0001 : Rule=Microsoft.Maintainability#CA1506, Target=Some.Namespace.ListExtensions : Collection was modified; enumeration operation may not execute.
오류에 link을하지만 난이 정보 주어진 내 확장 메서드를 해결하는 방법을 잘 모르겠어요 . 그것은 말합니다
결합 유형의 수를 줄이기 위해 유형이나 방법을 재 설계하십시오.
이 오류가 발생하는 이유는 누구이며 왜이 규칙을 위반하지 않도록 내 확장 프로그램을 수정하는 방법을 알고 있습니까?
감사합니다.
추신 : 누구나 언급하기 전에 HashSet 사용을 고려했지만 Compact Framework에는 HashSet이 없습니다.
왜 이것을 다운 그레이드 했습니까? 이것이 문제였습니다. –
심령 디버깅은 종종 감사하지 않습니다. –