【发布时间】:2018-06-16 16:08:08
【问题描述】:
我正在为检测类声明中是否缺少MessagePackObject 属性的分析器创建一个 CodeFixProvider。此外,我的属性需要有一个参数keyAsPropertyName,其值为true
[MessagePackObject(keyAsPropertyName:true)]
我已经完成了添加不带参数的属性(我的解决方法)
private async Task<Solution> AddAttributeAsync(Document document, ClassDeclarationSyntax classDecl, CancellationToken cancellationToken)
{
var root = await document.GetSyntaxRootAsync(cancellationToken);
var attributes = classDecl.AttributeLists.Add(
SyntaxFactory.AttributeList(SyntaxFactory.SingletonSeparatedList(
SyntaxFactory.Attribute(SyntaxFactory.IdentifierName("MessagePackObject"))
// .WithArgumentList(SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.AttributeArgument(SyntaxFactory.("keyAsPropertyName")))))))
// .WithArgumentList(...)
)).NormalizeWhitespace());
return document.WithSyntaxRoot(
root.ReplaceNode(
classDecl,
classDecl.WithAttributeLists(attributes)
)).Project.Solution;
}
但我不知道如何添加具有价值的参数的属性。有人可以帮帮我吗?
【问题讨论】:
-
你试过
... .WithArgumentList(SyntaxFactory.AttributeArgumentList(SyntaxFactory.SingletonSeparatedList(SyntaxFactory.AttributeArgument(null, SyntaxFactory.NameColon("keyAsPropertyName"), SyntaxFactory.LiteralExpression(SyntaxKind.TrueLiteralExpression)))))吗? -
@GeorgeAlexandria,是的,效果很好。请回答,我会标记的。