I'm a fan of the SyntaxFactory.Parse*
methods. (They're usually easier to understand)
You could use the following to generate the attribute you're looking for:
var name = SyntaxFactory.ParseName("MyAttribute");
var arguments = SyntaxFactory.ParseAttributeArgumentList("(\"some_param\")");
var attribute = SyntaxFactory.Attribute(name, arguments); //MyAttribute("some_param")
var attributeList = new SeparatedSyntaxList<AttributeSyntax>();
attributeList = attributeList.Add(attribute);
var list = SyntaxFactory.AttributeList(attributeList); //[MyAttribute("some_param")]
Alternatively you could use hand-crafted approach from Kirill's RoslynQuoter tool. But I think the fact that no one wants to write that code without his tool is telling... ;)
The manual approach looks like:
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 a StringLiteralExpression
as your argument.