当您有一个调用节点时,您可以查看其表达式是否为成员访问。如果调用是针对语句“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
}
}