How do I get the definition of a method using Roslyn?

  • How do I get the method declaration from only the MemberDeclarationSyntax object?

  • How to replace single and multiline comments from a method definition with empty.

    We can do this with SyntaxTriviaList.

    I have not assigned a SyntaxTriviaList object here. We have any method for getting information about the little things from the definition of the body.

  • How to get the method name yourself.

    private string GetMethodsInSourceFile(string fileName)
    {            
        SyntaxTree tree = SyntaxTree.ParseFile(fileName);
        var root = (CompilationUnitSyntax)tree.GetRoot();
        IEnumerable<Roslyn.Compilers.CSharp.SyntaxNode> syntaxNodes;
        syntaxNodes = from methodDeclaration in root.DescendantNodes()
         .Where(x => x is MethodDeclarationSyntax || x is PropertyDeclarationSyntax)
                      select methodDeclaration;
        if (syntaxNodes != null && syntaxNodes.Count() > 0)
        {
            foreach (MemberDeclarationSyntax method in syntaxNodes)
            {
                if (method != null)
                {                       
                    SyntaxTriviaList trivia;
                    if (trivia != null)
                    {
                        if(trivia.Count!=0)
                        {
                            foreach (SyntaxTrivia t in trivia)
                            {
                                if((t.Kind==SyntaxKind.DocumentationCommentTrivia) ||
                                    (t.Kind==SyntaxKind.SingleLineCommentTrivia) ||
                                    (t.Kind==SyntaxKind.MultiLineCommentTrivia))
                                {
                                    MemberDeclarationSyntax newAlterMethod=method.ReplaceTrivia(t, SyntaxTriviaList.Empty);
                                    if (newAlterMethod.ToFullString().ToUpper().Contains("PR_"))
                                    {
                                        methodsInSrceFileContainsProc.Add(newAlterMethod.ToString());
                                    }
                                }
                            }
                        }                        
                        else
                        {                              
                            methodsInSourceFile.Add(method.ToFullString());
                            if (method.ToFullString().ToUpper().Contains("PR_"))
                            {
                                methodsInSrceFileContainsProc.Add(method.ToString());
                            }
                        }                           
                    }            
    
                }
            }
        }
        return string.Empty;
    }
    
          

+3


source to share


1 answer


I'm assuming you don't need the full name. If you do this, you will have to use the SemanticModel API instead of the Syntax API.

To display the name of the method, enter MethodDeclarationSyntax

and use the property Identifier

.

To display the name of a property, enter PropertyDeclarationSyntax

and use the property Identifier

.

var tree = CSharpSyntaxTree.ParseText(@"
public class Sample
{
    public string FooProperty {get; set;}
   public void FooMethod()
   {
   }
}");

var members = tree.GetRoot().DescendantNodes().OfType<MemberDeclarationSyntax>();

foreach (var member in members)
{
    var property = member as PropertyDeclarationSyntax;
    if (property != null)
        Console.WriteLine("Property: " + property.Identifier);
    var method = member as MethodDeclarationSyntax;
    if (method != null)
        Console.WriteLine("Method: " + method.Identifier);
}

      

The next question is, "Why doesn't MemberDeclarationSyntax have a property Identifier

?"



MemberDeclarationSyntax

is the base class for more than just methods and properties. In particular, it is the base class for BaseFieldDeclarationSyntax

. Field declarations do not always have a clear identifier.

For example, what should be the identifier for the next field? He has two names.

class Sample
{
    private string fieldOne, fieldTwo;
}

      

Hope this clears it up for you.

+5


source







All Articles