列表扩展方法的 Microsoft.Maintainability 错误
所以我尝试为 List
创建一些基本的扩展方法。本质上我有一个 UniqueAdd 和 UniqueAddRange。它会在添加之前检查值是否存在,如果它已经在列表中,则不会添加它。这是代码:
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.
这是链接< /a> 错误,但我不确定如何根据此信息修复我的扩展方法。它说
尝试重新设计类型或方法以减少与其耦合的类型数量。
有谁知道为什么我会收到此错误以及如何修复我的扩展方法以便它们不违反此规则?
谢谢!
PS:在有人提到之前,我已经考虑过使用HashSet,但是紧凑框架中不存在HashSet。
So I have tried to create some basic extension methods for List
. Essentially I have a UniqueAdd and UniqueAddRange. It will check for the existence of a value before adding and if its already in the List it will not add it. Here is the code:
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);
}
}
}
And I get the following error when building:
CA0001 : Rule=Microsoft.Maintainability#CA1506, Target=Some.Namespace.ListExtensions : Collection was modified; enumeration operation may not execute.
Here is the link to the error but I'm not sure how to fix my extension methods given this information. It says to
Try to redesign the type or method to reduce the number of types to which it is coupled.
Does anyone know why I'm getting this error and how to fix my extension methods so they do not violate this rule?
Thanks!
PS: Before anyone mentions it, I have considered using a HashSet but HashSet does not exist in the compact framework.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我认为您的代码触发了 FxCop 中的错误,“集合已修改”是一个经典的错误。然后它决定它的错误是你的问题,catch(Exception) 风格。
寻找更新。我使用的不会抱怨你的代码(VS2010版本)。
I think your code triggered a bug in FxCop, "Collection was modified" is a classic oops. It then decided that its bug was your problem, catch(Exception) style.
Look for an update. The one I use doesn't complain about your code (VS2010 version).
它告诉您在枚举列表时正在更改列表。从您的代码中可以清楚地看出这一点(您在枚举的同时添加到列表中)。
怎么样:
或者,如果界面适合您的需求,请使用哈希集。它开箱即用,可以满足您的需求。It's telling you that you are changing the list as you are enumerating it. This is clear from your code (you're adding to the list at the same time as enumerating).
How about:
Or, if the interface is right for your needs, use a Hashset. It does what you want out of the box.