How to use SyntaxFactory for roslyn

330 Views Asked by At

I am trying to generate code using SyntaxFactory but stuck in converting one area Please see the code below I left comment where I am finding it difficult and used string manipulation.

The idea is there will be two variable removeExpression and getExpression which are of type ExpressionSyntax and finally both will be used to become the body of a anonymous function.

ExpressionSyntax removeExpression = SyntaxGenerator.MethodInvokeExpression(
  valueExpression,
  "RemoveAt",
  BinaryExpression(
   SyntaxKind.SubtractExpression,
   SyntaxGenerator.PropertyAccessExpression(valueExpression, nameof(Enumerable.Count)),
   atExpression
  )
 );

ExpressionSyntax getExpression = SyntaxGenerator.MethodInvokeExpression(valueExpression, nameof(Enumerable.First));

// Help me to convert the below line using SyntaxFactory.

IdentifierName("((Func<dynamic>)(() => { var res = " + getExpression.ToString() + ";" + removeExpression.ToString() + "; return res" + " ; }))(); ");

0

There are 0 best solutions below