用户控件中的 FxCop CA2000 警告

发布于 2024-09-05 18:07:05 字数 489 浏览 3 评论 0原文

在包含 UserControl 的 WebProject 上运行 FxCop 将导致该 UserControl 中的每个 ServerControl(标签、文本框...)出现 CA2000 警告(在对象上调用 System.IDisposable.Dispose)。

我明白为什么会发生这种情况。
用占位符替换“有问题的”ServerControls,然后在代码中添加控件(使用...结束使用)可能是解决这个问题的方法,但这并不总是一个选择。
但是,如果它们是不是“犹太洁食”为什么你可以首先将 ServerControls 放入你的 ascx/aspx 中?
我错过了什么吗?

如果像我的情况一样,您继承了大量相当复杂的 UserControls,我现在是否将每个“有问题的”控件添加到 GlobalSupperssions 文件中(右键单击会让人头脑麻木)?
我不想抑制所有 CA2000 警告,因为修复它们非常有意义,但对于 UserControls 中的 ServerControls 来说则不然。

Running FxCop on a WebProject that contains a UserControl will result in a CA2000 Warning (Call System.IDisposable.Dispose on object) for every ServerControl (Label, TextBox,...) in that UserControl.

I understand why this would happen.
Replacing the 'offending' ServerControls with a PlaceHolder and then adding the Controls in code (Using...End Using) might be a way around that, but it is not always an option.
But, if they are not 'kosher' why have ServerControls you can drop in your ascx/aspx in the first place ?
Am I missing something ?

If, like in my case, you inherit a sizeable collection of fairly complex UserControls, do I now add every 'offending' Control to the GlobalSupperssions file (that's a lot of mind numbing right-clicking) ?
I do not want to suppress all CA2000 warnings since it makes perfect sense to fix them, but not in the case of ServerControls in UserControls.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

乖乖 2024-09-12 18:07:05

嗯,每个人都真的喜欢一个可以告诉他们忘记调用 Dispose() 的工具。问题是:这样的工具确实很难实现。您所谈论的特定警告现在已被禁用并重新启用了几次。

显然,您收到的警告是无稽之谈,用户控件已经知道如何正确处置其成员控件。这不是自动化工具可以轻易检测到的事情。 dispose 调用与构造函数调用相距一百万英里,埋藏在 .NET 框架代码中,并混合了一些虚拟方法和事件处理程序。

真正评估这些警告,您必须切换到另一种工具:耳间工具。

Hmya, everybody really likes a tool that will tell them that they forgot to call Dispose(). Problem is: such a tool is really hard to implement. The specific warning you are talking about got disabled and re-enabled a few times now.

Obviously the warning you got is nonsense, a UserControl already knows how to properly dispose its member controls. It is just not something that an automated tool can easily detect. The dispose call is a million miles removed from the constructor call, buried in .NET framework code with a couple of virtual methods and event handlers thrown in the mix.

To really evaluate those warnings, you'll have to switch to another tool: the one between your ears.

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