I am a fan of SyntaxFactory.Parse*
methods. (They are usually easier to understand)
To generate an attribute, you can use the following:
var name = SyntaxFactory.ParseName("MyAttribute"); var arguments = SyntaxFactory.ParseAttributeArgumentList("(\"some_param\")"); var attribute = SyntaxFactory.Attribute(name, arguments);
Alternatively, you can use the hand tool from the Kirill RoslynQuoter tool. But I think that the fact that no one wants to write this code without its tool says ...;)
The manual approach looks like this:
var attributeArgument = SyntaxFactory.AttributeArgument( SyntaxFactory.LiteralExpression(SyntaxKind.StringLiteralExpression, SyntaxFactory.Token(default(SyntaxTriviaList), SyntaxKind.StringLiteralToken, "some_param", "some_param", default(SyntaxTriviaList)))); var otherList = new SeparatedSyntaxList<AttributeArgumentSyntax>(); otherList = otherList.Add(attributeArgument); var argumentList = SyntaxFactory.AttributeArgumentList(otherList); var attribute2 = SyntaxFactory.Attribute(name, argumentList);
In your example, you want to add StringLiteralExpression
as an argument.
source share