I am using Roslyn and I want to split the statement as below,
string stringVariable = "string";
int intVariable = 10;
Console.Write(stringVariable + string.Concat("string1","string2") + intVariable.ToString())
Console.Write()
stringVariable
string.Concat("string1","string2")
intVariable.ToString()
I have asked a question and got answer for splitting the expressions Splitting the Expression statements with Roslyn but this suggestion splits the string.Concat("string1", "string2")
as below,
string.Concat()
string1
string2
But i dont want to split the inner expressions, I need to keep the inner expressions as it is. How can I do this with Roslyn?
The expression
Console.Write(stringVariable + string.Concat("string1","string2") + intVariable.ToString())
is aInvocationExpressionSyntax
. The InvocationExpressionSyntax can be further splitted into expression and arguments.Here the expression part will have
Console.Write
and the argument part will havestringVariable + string.Concat("string1","string2") + intVariable.ToString()
.Now the argument will be the
BinaryExpressionSyntax
.We can split the BinaryExpressionSyntax by visiting the
SyntaxNodes
. So when Visiting we can just avoid traversing the "inner expressions" by identifyning its syntax( likeInvocationExpressionSyntax
,MemberAccessExpressionSyntax
..etc) .The code for visiting the Binary expression as above will be.
The class can be invoked by,
The nodeList will have the below result.