【发布时间】:2014-04-26 14:51:45
【问题描述】:
尝试转换:
const string maj = "variable";
在
const string MAJ = "variable";
我正在使用带有 CodeFix 的诊断程序。
我已经完成了诊断:
var localDeclarationConst = node as LocalDeclarationStatementSyntax;
if (localDeclarationConst != null &&
localDeclarationConst.Modifiers.Any(SyntaxKind.ConstKeyword)
)
{
foreach (VariableDeclaratorSyntax variable in localDeclarationConst.Declaration.Variables)
{
var symbol = model.GetDeclaredSymbol(variable);
if (symbol != null)
{
string varName = symbol.Name;
if (!varName.Equals(varName.ToUpper()))
{
addDiagnostic(Diagnostic.Create(Rule, localDeclarationConst.GetLocation(), "Les constantes doivent être en majusucle"));
}
}
}
}
但我找不到 CodeFix 的方法。这是我已经写的:
if (token.IsKind(SyntaxKind.ConstKeyword))
{
var ConstClause = (LocalDeclarationStatementSyntax)token.Parent;
var test = ConstClause.GetText();
var newConstClause = ConstClause.With //What with this With ??
var newRoot = root.ReplaceNode(ConstClause, newConstClause);
return new[] { CodeAction.Create("Mettre en maj", document.WithSyntaxRoot(newRoot)) };
}
如您所见,我正在寻找可以与 .With 一起使用的东西
编辑:
所以,我开始了解它是如何工作的。但是有一点我不知道它是如何工作的。让我解释一下:
if (token.IsKind(SyntaxKind.ConstKeyword))
{
var ConstClause = (VariableDeclaratorSyntax)token.Parent;
var test = ConstClause.Identifier.Text;
var newConstClause = ConstClause.ReplaceToken(SyntaxFactory.Identifier(test), SyntaxFactory.Identifier(test.ToUpperInvariant()));
var newRoot = root.ReplaceNode(ConstClause, newConstClause);
return new[] { CodeAction.Create("Make upper", document.WithSyntaxRoot(newRoot)) };
}
这就是我所做的。要访问变量的名称 (ConstClause.Identifier.Text),我使用 VariableDeclaratorSyntax 而不是 LocalDeclarationStatementSyntax。
但它不起作用。我有什么用?? 这将非常有帮助,因为我会知道如何更改变量的名称。我需要那个。
【问题讨论】:
-
如果您还没有查看过代码修复的模板,您可能想要查看。我们展示了如何在使用实际重命名 API 的代码修复中进行重命名。这样,我们将更新(聪明地!)您的常量的所有用途,而不仅仅是定义。
标签: c# roslyn diagnostics