Kim*_*gIn 3 c# roslyn roslyn-code-analysis
例如:
SqlCommand command = new SqlCommand();
SqlDataReader datareader = command.ExecuteReader();
Run Code Online (Sandbox Code Playgroud)
这里的调用节点是command.ExecuteReader()。我如何使用 roslyncommand从调用节点获取变量标识符令牌/节点?假设这个调用节点在它之前可以有许多其他方法调用,例如classA.methodA().methodB().classB.methodC(command.ExecuteReader()),因此获取标识符node.DescendantNodes可能没有用。我想到的解决办法是获得的SpanStartExecuteReader第一,然后通过获得的符号遵循command通过调用SymbolFinder.FindSymbolAtPosition与位置ExecuteReader.SpanStart - 2。但是我不确定这个解决方案是否可以处理每一种情况。我正在开发的应用程序是一个静态代码分析器。
当你有一个调用节点时,你可以看到它的表达式是否是成员访问。如果调用的声明“DoThis()”,那么没有一个成员访问,但如果调用是“x.DoThis()”再有就是因为“DoThis”的成员访问被称为与参考“ X”。
一旦你确认,是一个成员访问,你可以再拿到目标参考的表达-这是其成员被访问的参考。这个表达式可能是一个简单的名称标识符(例如“command”)或者它可能是另一个成员访问(例如“x.command”)或者它可能是另一个调用(例如“GetCommand()”)或者它可能是这些的组合。
用代码来说明——
private static void AnalyseInvocation(SyntaxNodeAnalysisContext context)
{
var invocation = (InvocationExpressionSyntax)context.Node;
var memberAccess = invocation.Expression as MemberAccessExpressionSyntax;
if ((memberAccess == null) || (memberAccess.Name.Identifier.ValueText != "ExecuteReader"))
return;
if (memberAccess.Expression is IdentifierNameSyntax)
{
// The target is a simple identifier, the code being analysed is of the form
// "command.ExecuteReader()" and memberAccess.Expression is the "command"
// node
}
else if (memberAccess.Expression is InvocationExpressionSyntax)
{
// The target is another invocation, the code being analysed is of the form
// "GetCommand().ExecuteReader()" and memberAccess.Expression is the
// "GetCommand()" node
}
else if (memberAccess.Expression is MemberAccessExpressionSyntax)
{
// The target is a member access, the code being analysed is of the form
// "x.Command.ExecuteReader()" and memberAccess.Expression is the "x.Command"
// node
}
}
Run Code Online (Sandbox Code Playgroud)