简体   繁体   中英

How to get method definition using 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; } 

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.

var body = member.Body.ToString();

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM