C# 使用SyntaxGenerator向属性添加属性

C# 使用SyntaxGenerator向属性添加属性,c#,syntax,attributes,roslyn,C#,Syntax,Attributes,Roslyn,我正在使用SyntaxGenerator动态创建一些代码。我需要向属性添加一个属性。以下是我所拥有的: var g = // some SyntaxGenerator var attributes = new List<SyntaxNode> { g.Attribute("System.Xml.Serialization.XmlAttributeAttribute") }; var property = g.PropertyDeclaration(

我正在使用SyntaxGenerator动态创建一些代码。我需要向属性添加一个属性。以下是我所拥有的:

var g = // some SyntaxGenerator
var attributes = new List<SyntaxNode>
{
   g.Attribute("System.Xml.Serialization.XmlAttributeAttribute")
};
var property = g.PropertyDeclaration(
                name, // The property name
                type, // A SyntaxNode resembling the type
                accessibility: Accessibility.Public,
                getAccessorStatements: getModifiers,
                setAccessorStatements: setModifiers);

                g.AddAttributes(property, attributes); // Does nothing

如何添加属性?

事实证明,我忘记了做这个简单的赋值,因为AddAttributes创建了SyntaxNode的一个新实例:

property = g.AddAttributes(property, attributes);

您可以将所需的代码粘贴到,将其解析为成员,然后点击generate。@Xiaoy312不幸的是,我被语法生成器卡住了。那里的API使用SyntaxFactory。
property = g.AddAttributes(property, attributes);