禁用/修复 .Designer.cs 文件中的代码分析警告

发布于 2024-11-28 15:51:17 字数 361 浏览 2 评论 0 原文

我广泛使用DataVisualization.Charting.Chart,并且大部分情况下它都可以工作。但是,我经常运行代码分析,并处理了我自己的所有警告。但是,使用图表的 *.Designer.cs 文件中有大约 30 个 CA2000(未沿着所有异常路径处理的对象)。设计器文件生成几乎所有图表代码,并且几乎所有图表元素都实现了IDisposable。我在项目首选项中检查了“抑制生成代码的结果”,但它仍然会执行此操作。

有没有办法解决这个问题,而无需手动创建图表对象,并且无需对该类中的其余代码禁用代码分析?有没有办法对所有 .Designer.cs 文件禁用它?或者,是否有解决方案可以通过让设计器代码负责处理来正确删除这些警告?

I am using DataVisualization.Charting.Chart extensively, and for the most part it is working. However, I've been running Code Analysis frequently, and have all my own warnings taken care of. But, there are about 30 CA2000 (object not disposed along all exception paths) in the *.Designer.cs files that use the charting. The Designer files are generating pretty much all the charting code, and almost all the charting elements implement IDisposable. I have "Suppress results from generated code" checked in the project preferences, but it still does it.

Is there any way to fix this, without having to manually create the chart objects, and without disabling Code Analysis for the rest of the code in that class? Is there a way to disable it for all .Designer.cs files? Or, is there a solution to remove these warnings correctly by making the designer code take care of disposal?

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

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

发布评论

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

评论(4

乖乖兔^ω^ 2024-12-05 15:51:17

相当多的开发人员似乎遇到了这个问题,但运气不佳,所以+1 是一个好问题!

一个可能的解决方案是编写一个方法来覆盖 CA2000 并在设计器文件中检测到警告时抑制规则,这是一个好的开始:

在 Visual Studio 2010 中编写自定义代码分析规则

否则请参阅本线程末尾的评论,MSFT工程师提到要记录 Connect 调用:http://blogs.msdn.com/b/codeanalysis/archive/2010/03/22/what-s-new-in-code-analysis-for-visual-studio-2010.aspx< /a>

A fair few developers appear to have encountered this without a luck, so +1 for a good question!

A possible solution is to write a method that override's CA2000 and suppresses the rule if the warning is detected in a designer file, here's a good start:

Writing Custom Code Analysis Rules in Visual Studio 2010

Otherwise see the comments at the end of this thread, MSFT engineers mention to log a Connect call: http://blogs.msdn.com/b/codeanalysis/archive/2010/03/22/what-s-new-in-code-analysis-for-visual-studio-2010.aspx

不醒的梦 2024-12-05 15:51:17

我知道我迟到了,但就这样吧。

我猜这些警告都是针对 InitializeComponent 方法中的代码发出的?如果是这样,那么您是否考虑过修改位于 Common7\IDE\ItemTemplates 文件夹中的模板文件?您可以在其中的方法上添加 GeneratedCode 属性。由于该属性仅在其上设置,因此同一类中的所有其他代码仍将通过代码分析进行检查。

以下是 Form 设计器文件的示例:

namespace $rootnamespace$
{
    partial class $safeitemrootname$
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        [System.CodeDom.Compiler.GeneratedCode("Windows Form Designer generated code", "1.0.0.0"), System.Diagnostics.DebuggerNonUserCode()]
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "$safeitemrootname$";
        }

        #endregion
    }
}

I know I'm late to this but here goes.

I'm guessing these warnings are all emitted for code within the InitializeComponent method? If so then have you considered modifying the template files located in Common7\IDE\ItemTemplates folder? You could add the GeneratedCode attribute on the method in those. Since the attribute will be set only on it, all your other code within the same class will still get checked by code analysis.

Here's an example for Form designer file:

namespace $rootnamespace$
{
    partial class $safeitemrootname$
    {
        /// <summary>
        /// Required designer variable.
        /// </summary>
        private System.ComponentModel.IContainer components = null;

        /// <summary>
        /// Clean up any resources being used.
        /// </summary>
        /// <param name="disposing">true if managed resources should be disposed; otherwise, false.</param>
        protected override void Dispose(bool disposing)
        {
            if (disposing && (components != null))
            {
                components.Dispose();
            }
            base.Dispose(disposing);
        }

        #region Windows Form Designer generated code

        /// <summary>
        /// Required method for Designer support - do not modify
        /// the contents of this method with the code editor.
        /// </summary>
        [System.CodeDom.Compiler.GeneratedCode("Windows Form Designer generated code", "1.0.0.0"), System.Diagnostics.DebuggerNonUserCode()]
        private void InitializeComponent()
        {
            this.components = new System.ComponentModel.Container();
            this.AutoScaleMode = System.Windows.Forms.AutoScaleMode.Font;
            this.Text = "$safeitemrootname$";
        }

        #endregion
    }
}
晨敛清荷 2024-12-05 15:51:17

只需将 [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDispose", MessageId = "..."] 添加到 *.Designer 中的 Dispose 方法即可。 。

我刚刚做了,我发现 VS 2012 足够聪明,即使在设计器中发生更改时重写文件也能将其保留在那里

Simply add a [SuppressMessage("Microsoft.Usage", "CA2213:DisposableFieldsShouldBeDisposed", MessageId = "..."] to the Dispose method in your *.Designer.cs file.

I just did, and I've found out that VS 2012 is clever enough to keep it there even when rewriting the file when something was changed in the designer.

北音执念 2024-12-05 15:51:17

您是否尝试过在项目的“代码分析”属性页中将“抑制生成代码的结果”属性值切换为 true?此选项是忽略生成代码中问题的标准机制。

也就是说,生成的代码是将要执行的代码,因此忽略其违规不一定是个好主意。鉴于 CA2000 的“噪音”,您可能希望考虑禁用该规则。

Have you tried toggling the "Suppress results from generated code" property value to true in the Code Analysis property page for your project(s)? This option is the standard mechanism for ignoring problems in generated code.

That said, generated code is code that will be executed, so ignoring its violations is not necessarily a great idea. Given the "noisiness" of CA2000, you may wish to consider disabling the rule instead.

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