alface
alface

Reputation: 89

Use Roslyn to get return statements ignoring the ones inside lambda expressions

I am trying to use roslyn to get the return statements of a method, for that I am using this:

var returns = methods.DescendantNodes().OfType<ReturnStatementSyntax>();

That works, it gives me all the return statements. But now, I want to get all the returns ignoring the ones inside lambda expressions.

How can I do that?

Is there any property that indicates that?

Upvotes: 0

Views: 299

Answers (1)

G&#246;rkem &#214;zer
G&#246;rkem &#214;zer

Reputation: 564

I am not sure but you can try something similar like this:

method.DescendantNodes().OfType<ReturnStatementSyntax>().Where(
     rs => !(rs.AncestorsAndSelf().OfType<LambdaExpressionSyntax>().Any())
);

I hope this will solve your problem.

Upvotes: 5

Related Questions