列表扩展方法的 Microsoft.Maintainability 错误

发布于 2024-12-07 17:31:27 字数 1721 浏览 2 评论 0原文

所以我尝试为 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

感性 2024-12-14 17:31:27

我认为您的代码触发了 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).

2024-12-14 17:31:27

它告诉您在枚举列表时正在更改列表。从您的代码中可以清楚地看出这一点(您在枚举的同时添加到列表中)。

怎么样:

public static void UniqueAddRange<T>(this List<T> list, IEnumerable<T> values)
{
    list.AddRange(values.Except(list));
}

或者,如果界面适合您的需求,请使用哈希集。它开箱即用,可以满足您的需求。

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:

public static void UniqueAddRange<T>(this List<T> list, IEnumerable<T> values)
{
    list.AddRange(values.Except(list));
}

Or, if the interface is right for your needs, use a Hashset. It does what you want out of the box.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文