开发者

What is the utility of the attribute GeneratedCodeAttribute in C #?

开发者 https://www.devze.com 2022-12-22 13:46 出处:网络
I generated some of my C# code with an external tool. Each generated class has an attribute GeneratedCodeAttribute. Why is my generator creating this att开发者_Python百科ribute?This attribute was set

I generated some of my C# code with an external tool. Each generated class has an attribute GeneratedCodeAttribute. Why is my generator creating this att开发者_Python百科ribute?


This attribute was set because this code is generated by tool, not by human :) what is use of it you might ask? MSDN tells us:

The GeneratedCodeAttribute class can be used by code analysis tools to identify computer-generated code, and to provide an analysis based on the tool and the version of the tool that generated the code.


The first link is its documentation and the second link is a detailed description of what this is for, why code generators produce it, and how code analyzers consume it.

http://msdn.microsoft.com/en-us/library/system.codedom.compiler.generatedcodeattribute.aspx

and

https://blogs.msdn.microsoft.com/codeanalysis/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute/

Does that answer your question?


It is most probably used by the generator to find back the elements it created, in order to perform updates for example. Beware if you modify generated code : depending on the tool behaviour, you may loose your modifications on a further update.


One potential use is that Some coverage tools can skip code based on specified attributes. You can tell NCover to ignore code with this attribute.


Here's the relevant text from Microsoft blog article Correct usage of the CompilerGeneratedAttribute and the GeneratedCodeAttribute, in case it eventually disappears from their site (which is common).


Correct usage of the CompilerGeneratedAttribute and the GeneratedCodeAttribute

04/27/2007

Both Code Analysis, FxCop and Code Metrics make extensive use of CompilerGeneratedAttribute and GeneratedCodeAttribute to distinguish between user-written code and tool and compiler generated code.

The following describes this behavior:

Code Analysis in Visual Studio 2005 and FxCop 1.35

  • Tool Generated. Raises warnings against nearly all tool generated code. Uses an algorithm to turn off particular rules against code marked with GeneratedCodeAttribute and generated by specific code generators.

Code Analysis in Visual Studio 2008 and FxCop 1.36

  • Tool Generated. Does not raise warnings against code marked with the GeneratedCodeAttribute if Suppress results from generated code is turned on (the default).

Code Metrics in Visual Studio 2008

  • Tool Generated. Does not display or generate metrics for code marked with the GeneratedCodeAttribute.

Unfortunately, there are many cases of incorrect usage of these attributes both internally and externally of Microsoft, and this blog post is an attempt to make things a little clearer in the correct application of these attributes.

...

GeneratedCodeAttribute

This attribute is for use by custom tools that generate code. It should only be applied to code that is re-generated over and over and should not be used by templates that the user is expected to modify. Nor should it be applied at the type level if the type being generated is a partial class. In this situation, it should be applied only against the individual members that are contained within the generated part of the type.

For example, the following shows the incorrect usage of this attribute (it is being applied at the type level to a partial class):

[GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0" )]
internal sealed partial class Settings : ApplicationSettingsBase
{
    private static Settings defaultInstance = ((Settings)(ApplicationSettingsBase.Synchronized(new Settings())));

    public static Settings Default
    {
        get { return defaultInstance; }
    }
    
    [UserScopedSettingAttribute()]
    [DebuggerNonUserCodeAttribute()]
    [DefaultSettingValueAttribute("")]
    public string MySetting
    {
        get { return ((string)(this["MySetting"])); }
        set { this["MySetting"] = value; }
    }

}

The following shows the correct usage of this attribute:

internal sealed partial class Settings : ApplicationSettingsBase
{
    [GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0" )]
    private static Settings defaultInstance = ((Settings)(ApplicationSettingsBase.Synchronized(new Settings())));
    
    [GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
    public static Settings Default
    {
    get { return defaultInstance; }
    }
    
    [GeneratedCodeAttribute("Microsoft.VisualStudio.Editors.SettingsDesigner.SettingsSingleFileGenerator", "9.0.0.0")]
    [UserScopedSettingAttribute()]
    [DebuggerNonUserCodeAttribute()]
    [DefaultSettingValueAttribute("")]
    public string MySetting
    {
        get { return ((string)(this["MySetting"])); }
        set { this["MySetting"] = value; }
    }
}
0

精彩评论

暂无评论...
验证码 换一张
取 消