Disabling/Fixing Code Analysis warnings from .Designer.cs files

后端 未结 4 1405
忘了有多久
忘了有多久 2021-02-13 14:44

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

相关标签:
4条回答
  • 2021-02-13 15:33

    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
        }
    }
    
    0 讨论(0)
  • 2021-02-13 15:34

    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.

    0 讨论(0)
  • 2021-02-13 15:41

    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

    0 讨论(0)
  • 2021-02-13 15:47

    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.

    0 讨论(0)
提交回复
热议问题