我正在尝试使用.NET编译器平台(roslyn)实现代码重构。但是,出于某种原因,我不能使ObjectCreationExpression与ArgumentList一起使用。这是我的方法(整个代码可以找到here):
private aync Task<Document> AddGuardAsync(Document document, ParameterSyntax parameter, BaseMethodDeclarationSyntax methodDeclaration, CancellationToken cancellationToken)
{
BinaryExpressionSyntax binaryExpression = SyntaxFactory.BinaryExpression(SyntaxKind.EqualsExpression,
SyntaxFactory.IdentifierName(parameter.Identifier),
SyntaxFactory.LiteralExpression(SyntaxKind.NullLiteralExpression));
NameOfExpressionSyntax nameOfExp = SyntaxFactory.NameOfExpression(
"nameof",
SyntaxFactory.ParseTypeName(parameter.Identifier.Text));
SeparatedSyntaxList<ArgumentSyntax> argsList = new SeparatedSyntaxList<ArgumentSyntax>();
argsList.Add(SyntaxFactory.Argument(nameOfExp));
ObjectCreationExpressionSyntax objectCreationEx = SyntaxFactory.ObjectCreationExpression(
SyntaxFactory.ParseTypeName(nameof(ArgumentNullException)),
SyntaxFactory.ArgumentList(argsList),
null);
ThrowStatementSyntax throwStatement = SyntaxFactory.ThrowStatement(objectCreationEx);
IfStatementSyntax ifStatement = SyntaxFactory
.IfStatement(SyntaxFactory.Token(SyntaxKind.IfKeyword), SyntaxFactory.Token(SyntaxKind.OpenParenToken), binaryExpression, SyntaxFactory.Token(SyntaxKind.CloseParenToken), throwStatement, null)
.WithAdditionalAnnotations(Formatter.Annotation);
SyntaxNode root = await document.GetSyntaxRootAsync(cancellationToken);
SyntaxNode newRoot = root.InsertNodesBefore(methodDeclaration.Body.ChildNodes().First(), new[] { ifStatement });
return document.WithSyntaxRoot(newRoot);
}
这奇怪地产生了以下没有nameof
参数的代码修复建议(即使没有大括号):
我在这里缺少什么?
答案 0 :(得分:9)
SeparatedSyntaxList<T>
是不可变的
调用.Add()
会返回一个新列表,并添加您的令牌。
您忽略了这个新列表,因此您的令牌永远不会出现在任何地方。
你想要
argsList = argsList.Add(SyntaxFactory.Argument(nameOfExp));