0

Is it possible to get the property that an attribute refers to using Roslyn syntax analysis? I was able to get the attribute name as follows:

using System;
using System.Collections.Generic;
using System.IO;
using Microsoft.CodeAnalysis;
using Microsoft.CodeAnalysis.CSharp;
using Microsoft.CodeAnalysis.CSharp.Syntax;
using System.Linq;

namespace Project1
{
   internal class Program
   {
      static void Main(string[] args)
      {
         string sourceCode = File.ReadAllText("TestAttributes.cs");
         SyntaxTree tree = CSharpSyntaxTree.ParseText(sourceCode);
         CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
         AttributeListSyntax node = root.DescendantNodes().OfType<AttributeListSyntax>().First();
         IdentifierNameSyntax identifierName  = node.Attributes[0].Name as IdentifierNameSyntax;
         Console.WriteLine(identifierName.Identifier.Text);
      }
   }
}

With TestAttributes.cs containing the following:

using System.ComponentModel.DataAnnotations;

namespace Project1
{
   public class Person
   {
      [Range(18, 120, ErrorMessage = "Age must be between 18 and 120")]
      public int Age { get; set; }
   }
}

However, in this case I'd like to do either of the following:

  • Get the node representing the attribute range from the node representing the property Age
  • Get the node representing the property Age from the node representing the attribute range

Is there any way to do this with the Roslyn syntax analysis API? I'd like to avoid semantic analysis if possible.

Edit

Here's what I tried after @Selvin's comment:

namespace Project1
{
   internal class Program
   {
      static void Main(string[] args)
      {
         string sourceCode = File.ReadAllText("C:\\Users\\Amine.Aboufirass\\Desktop\\TEMP\\user-story-425155\\syntax-analysis\\Project1\\TestAttributes.cs");
         SyntaxTree tree = CSharpSyntaxTree.ParseText(sourceCode);
         CompilationUnitSyntax root = tree.GetCompilationUnitRoot();
         AttributeListSyntax node = root.DescendantNodes().OfType<AttributeListSyntax>().First();
         IdentifierNameSyntax identifierName  = node.Attributes[0].Name as IdentifierNameSyntax;
         IdentifierNameSyntax correspondingIdentifierName = node.Parent as IdentifierNameSyntax;

         Console.WriteLine(identifierName.Identifier.Text);
         Console.WriteLine(correspondingIdentifierName.Identifier.Text);
      }
   }
}

Unfortunately I'm now getting a runtime error.

7
  • Attribute's parent is propertie's and parent of property is class .... Commented Feb 10, 2023 at 13:41
  • Sorry, I made a mistake in my OP. I meant to say the how to get the property that the attribute refers to, Edited. Commented Feb 10, 2023 at 13:46
  • again, you have already node which is attribute... node.Parent is property Age ... if you would have Age's PropertyDeclarationSyntax some element of node of type AttributeListSyntax would be again attribute ... what is the definitive goal? Commented Feb 10, 2023 at 13:50
  • Ok I tried something based on your suggestion. Please see original post. Commented Feb 10, 2023 at 14:01
  • Node pareent would be PropertyDeclarationSyntax not IdentifierNameSyntax use ((PropertyDeclarationSyntax)node.Parent).Identifier Commented Feb 10, 2023 at 14:04

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.