How to get method definition using Roslyn?

旧城冷巷雨未停 提交于 2019-12-07 10:03:50

问题


  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;
    }
    

回答1:


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.




回答2:


var body = member.Body.ToString();

var fullMethodName = member.ToString().Replace(body, "");


来源:https://stackoverflow.com/questions/26436368/how-to-get-method-definition-using-roslyn

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!