Reputation: 18465
I generated some of my C# code with an external tool. Each generated class has an attribute GeneratedCodeAttribute. Why is my generator creating this attribute?
Upvotes: 22
Views: 14761
Reputation: 13008
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).
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
Code Analysis in Visual Studio 2008 and FxCop 1.36
Code Metrics in Visual Studio 2008
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; }
}
}
Upvotes: 1
Reputation: 660024
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
Does that answer your question?
Upvotes: 7
Reputation: 46173
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.
Upvotes: 1
Reputation: 60065
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.
Upvotes: 18
Reputation: 2715
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.
Upvotes: 1