[使用SyntaxGenerator向属性添加属性

问题描述 投票:0回答:1

我正在使用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

我希望属性显示如下:

[System.Xml.Serialization.XmlAttributeAttribute()]
    public string Id
    {
        get
        {
            return this.id;
        }
        set
        {
            this.id = value;
        }
    }

如何添加属性?

c# syntax attributes roslyn
1个回答
0
投票

原来我忘了这样做(由于AddAttributes()创建了SyntaxNode的新实例,所以很简单:

property = g.AddAttributes(property, attributes);
© www.soinside.com 2019 - 2024. All rights reserved.