My problem is when i call remove nodes, or even remove node. I get a ArgumentNullException.
I'm trying to create a codefix that removes ".Result" and then add await.
I'm stuck at the removing .Result part
Here's my code
private const string sampleCode =
@"
using System.Threading.Tasks;
internal static class Class1
{
public static int Wrong()
{
return Delay1().Result;
}
async static Task<int> Delay1() { await Task.Delay (1000); return 1; }
}
";
async Task Main()
{
SyntaxTree tree = CSharpSyntaxTree.ParseText(sampleCode);
var root = (CompilationUnitSyntax)tree.GetRoot();
var node = root.DescendantNodesAndSelf().First(x => x is MemberAccessExpressionSyntax);
foreach (var syntaxNode in node.ChildNodes())
{
if (syntaxNode is IdentifierNameSyntax identifierNameSyntax)
{
if (identifierNameSyntax.Identifier.Text == "Result")
{
nodesToRemove = new[] { previousNode, syntaxNode };
break;
}
}
previousNode = syntaxNode;
}
newNode = node.RemoveNodes(nodesToRemove, SyntaxRemoveOptions.KeepNoTrivia);
// throws exception on above line
}
if the pervious node is first (which is the ".") it throws
Value cannot be null. (Parameter 'expression')
if the order is the other way its
Value cannot be null. (Parameter 'name')
Any help appreciated