简体   繁体   中英

Get SqlCommand local var declaration using Roslyn

I'm trying to get the first SqlCommand local declaration from a method body.

Here is my code method declaration visitor:

public override void VisitMethodDeclaration(MethodDeclarationSyntax node)
{
    var sqlCommandDeclaration = node.Body.Statements
            .Where(x => x.IsKind(LocalDeclarationStatement))
            .Cast<LocalDeclarationStatementSyntax>()
            .FirstOrDefault(x => x.Declaration.Type.GetText().ToString() == "SqlCommand");

    base.VisitMethodDeclaration(node);
}

Could someone help me with it?

    var firstOne = node.Body.Statements
        .OfType<LocalDeclarationStatementSyntax>()
        .FirstOrDefault(syntax => syntax.Declaration.Type.ToString() == "SqlCommand");

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