Search code examples
c#roslynanalyzer

Determine if a property is an auto property


I'm trying to figure out if a property is an auto property i.e. public int Foo { get; set; }

Stared for a while at PropertyDeclarationSyntax and IPropertySymbol but did not find anything.

Guess an alternative is an extension method that evaluates if get & set does not contain any statements is one way but it does not feel very elegant.


Solution

  • Check whether any of the AccessorDeclarationSyntaxes in the PropertyDeclarationSyntax's AccessorList have a non-null Body.

    You can see this by looking at any property declaration using the Syntax Visualizer (from the Roslyn SDK extension).