Roslyn - how detect code files that are auto-generated?

Viewed 280

Is it possible to detect if Microsoft.CodeAnalysis.Document has been auto-generated by a tool?

For example:

foreach(var document in project.Documents)
{
    if (!document.IsGeneratedCode())
    {
        // TODO
    }

}

Looking at the source code on GitHub, I found out that extension methods IsGeneratedCode and IsGeneratedCodeAsync exist, but only as internal helpers. Implementation calls IGeneratedCodeRecognitionService language service that is also internal.

Is there any way to use similar functionality in external console application?

1 Answers

Not ideal, but you could copy the internal logic. Basically, code is considered "generated" if:

  • File begins with auto-generated comment // <autogenerated />
  • File name starts with TemporaryGeneratedFile_
  • File name ends with .designer, .generated, .g, or .g.i right before the extension
Related