Visual Studio 代码分析规则 - “不要公开通用列表”
如果我所有的方法都需要公开一个集合,那么我需要使用 Linq 扩展 .ToList(),几乎所有我需要在所有代码中使用列表或用户集合的地方。
如果是这样的话,.ToList() 会忽略该规则,对吧?或者是否有一种技术,例如复制列表或某些内容来修复违规并仍然返回列表?
IF all my methods, need to expose a collection, then I need to user the Linq Extension .ToList(), almost everywhere I need to use lists, or user Collections in all my code.
If that’s the case, .ToList() is ignoring the rule right? Or is there a technique like copying the list o something to fix the violation and still return a list?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我禁用该规则,因为我觉得它不是有效的。如果您想要返回一个包含
O(1)
计数且不是对内部字段的直接引用的集合,List
是最佳选择。我不太了解你的情况,但听起来你有一个方法可以返回一些内部数据的 LINQ 查询。如果是这种情况,那么对数据使用
.ToList()
是合适的,因为您可能不希望将来对内部字段的修改影响方法的返回值。在这种情况下,没有理由不将其公开为List
。I disable that rule because I don't feel like it's a valid one. If you want to return a collection which contains an
O(1)
count and is not a direct reference to an internal field,List<T>
is the best choice.I don't deeply understand your case here but it sounds like you have a method which returns a LINQ query over some internal data. If that's the case then using a
.ToList()
on the data is appropriate since you likely don't want future modifications of your internal fields to affect the return value of a method. In that case, there is no reason to not expose it as aList<T>
.这个规则确实可能很吵,但是有一些非常有效的理由来避免在库代码中使用
List
。这一切都取决于上下文。在禁用规则或抑制给定事件之前,需要考虑以下几点:List
对于输入参数来说通常是一个糟糕的选择,因为它迫使调用者不必要地复制数据。我见过很多代码将参数声明为List
或T[]
,而IEnumerable
就足够了。List
对于属性来说也可能是一个糟糕的选择。考虑以下替代方案:目的是调用者可以通过修改集合来更改课程的先决条件。在这种情况下,如果我们使用
List
,则当自List< 以来先决条件发生更改时,
Course
类无法收到通知。 /code> 不提供任何修改回调。因此,在此上下文中使用List
就像拥有任意多个公共字段。另一方面,我们可以对Collection
进行子类化并重写其虚函数以获取更改通知。当集合的完整所有权转移给调用者时,
List
作为返回值效果最佳。这就是为什么Enumerable.ToList()
实际上是完全合理的,并且没有违反规则的精神。现在我想了一下,允许
List
作为方法的返回值,但继续标记List
属性和参数可能会大大改善规则的信噪比...This rule can indeed be noisy, but there are some very valid reasons to avoid
List<T>
in library code. It all depends on the context. Here are a few things to consider before disabling the rule or suppressing a given occurrence:List<T>
is often a poor choice for input parameters as it forces callers to copy data unnecessarily. I have seen lots of code that declares parameters asList<T>
orT[]
whenIEnumerable<T>
would suffice.List<T>
can be a poor choice for properties as well. Consider the following alternatives:The intention is that the caller can change a course's prerequistes by modifying the collection. In that case, if we use
List<Course>
, there is no way for theCourse
class to be notified when the prerequisites change sinceList<T>
does not provide any modification callbacks. As such, usingList<T>
in this context is like having arbitrarily many public fields. On the other hand, we can subclassCollection<T>
and override its virtuals to be notified of changes.List<T>
works best as a return value when complete ownership of the collection is transferred to the caller. This is whyEnumerable.ToList()
is actually perfectly reasonable and it does not violate the spirit of the rule.Now that I think about it, allowing
List<T>
as a return value from methods, but continuing to flagList<T>
properties and parameters would probably greatly improve the rule's signal to noise ratio...请记住,所有这些规则都是为框架开发人员编写的。其中许多可能不适合,除非您也在编写框架。
您必须对每条规则做出判断,看看它是否适合您的情况。我喜欢使用分析,因为它有时确实会发现一些错误,但我总是最终禁用某些规则(例如,我经常使用
catch Exception
作为最终的包罗万象,只是因为我需要记录各种错误,即使它们无法处理)。Remember that all these rules were written for framework developers. Many of them are likely to be unsuitable unless you're also writing a framework.
You'll have to make a judgement call for every rule to see if it's valid for your circumstances. I like to use the analysis since it does find some bugs sometimes, but I always end up disabling certain rules (for example, I quite often have a
catch Exception
as a final catch-all just because I need to log all kinds of errors even if they can't be handled).