Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get method definition using Roslyn?

Tags:

c#

roslyn

  1. How to get method declaration alone from MemberDeclarationSyntax object?

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

    Can we do this with SyntaxTriviaList.

    Here i didn't assigned any object to SyntaxTriviaList. Do we have any method for
    getting trivia info from definition of body.

  3. How to get Method Name alone.

    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;
    }
    
like image 783
Siva Avatar asked Oct 18 '14 04:10

Siva


2 Answers

I'm assuming you don't need the fully qualified name. If you do, you'll have to use the SemanticModel API instead of the Syntax API.

To display the name of a method, cast to MethodDeclarationSyntax and use the Identifier property.

To display the name of a property, cast to PropertyDeclarationSyntax and use the Identifier property.

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 followup question is "Why doesn't MemberDeclarationSyntax have an Identifier property?

MemberDeclarationSyntax is the base class for more than just methods and properties. In particular, it's the base class for BaseFieldDeclarationSyntax. Field declarations don't always have a clear identifier.

For example, what should be identifier for the following field be? It has two names.

class Sample
{
    private string fieldOne, fieldTwo;
}

Hopefully this clears it up for you.

like image 193
JoshVarty Avatar answered Oct 16 '22 16:10

JoshVarty


var body = member.Body.ToString();

var fullMethodName = member.ToString().Replace(body, "");
like image 22
arch Avatar answered Oct 16 '22 16:10

arch