简体   繁体   English

如何使用Roslyn获取方法定义?

[英]How to get method definition using Roslyn?

  1. How to get method declaration alone from MemberDeclarationSyntax object? 如何从MemberDeclarationSyntax对象单独获取方法声明?

  2. How to replace single line and multiline comments from a method definition with empty. 如何用空的方法定义替换单行和多行注释。

    Can we do this with SyntaxTriviaList. 我们可以使用SyntaxTriviaList执行此操作。

    Here i didn't assigned any object to SyntaxTriviaList. 这里我没有为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. 如果这样做,您将不得不使用SemanticModel API而不是语法API。

To display the name of a method, cast to MethodDeclarationSyntax and use the Identifier property. 要显示方法的名称,请转换为MethodDeclarationSyntax并使用Identifier属性。

To display the name of a property, cast to PropertyDeclarationSyntax and use the Identifier property. 要显示属性的名称,请转换为PropertyDeclarationSyntax并使用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 followup question is "Why doesn't MemberDeclarationSyntax have an Identifier property? 后续问题是“为什么MemberDeclarationSyntax没有Identifier属性?

MemberDeclarationSyntax is the base class for more than just methods and properties. MemberDeclarationSyntax不仅仅是方法和属性的基类。 In particular, it's the base class for BaseFieldDeclarationSyntax . 特别是,它是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, "");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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