呼び出しノードがある場合、その式がメンバー アクセスであるかどうかを確認できます。呼び出しがステートメント「DoThis()」に対するものである場合、メンバー アクセスはありませんが、呼び出しが「x.DoThis()」に対するものである場合、参照に対して「DoThis」が呼び出されているため、メンバー アクセスがあります。バツ"。
メンバー アクセスがあることを確認すると、ターゲット参照の式を取得できます。これは、メンバーがアクセスされている参照です。この式は、単純な名前識別子 (例: "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
}
}