views:

156

answers:

4

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?

+1  A: 

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.

Seb
i doubt because usually generated code and normal code are not mixed in same file.
Andrey
+4  A: 

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.

Andrey
NCover actually can be taught to take this attribute into account when you compute your unit test coverage.
jdv
yes and that fits into definition, NCover is "code analysis tools"
Andrey
A: 

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.

Mike Two
+2  A: 

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

http://blogs.msdn.com/fxcop/archive/2007/04/27/correct-usage-of-the-compilergeneratedattribute-and-the-generatedcodeattribute.aspx

Does that answer your question?

Eric Lippert
LMBTFY - let me bing that for you
Michael Valenty