Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/266.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何让Roslyn正确格式化XML文档注释?_C#_Roslyn - Fatal编程技术网

C# 如何让Roslyn正确格式化XML文档注释?

C# 如何让Roslyn正确格式化XML文档注释?,c#,roslyn,C#,Roslyn,我使用SyntaxRewriter将类从旧库转换为新库,这基本上需要查找具有给定属性的类,然后重写遵循特定约定的属性。重写器的总体框架如下所示: class PropertyConverter : SyntaxRewriter { public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node) { if (!MeetsUpdateCriteria(node))

我使用
SyntaxRewriter
将类从旧库转换为新库,这基本上需要查找具有给定属性的类,然后重写遵循特定约定的属性。重写器的总体框架如下所示:

class PropertyConverter : SyntaxRewriter
{
    public override SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax node)
    {
        if (!MeetsUpdateCriteria(node)) return base.VisitPropertyDeclaration(node);

        // these implementations simply return a string
        var name = FigureOutName(node.Identifier);
        var propertyType = FigureOutType(node.Type);

        var getter = Syntax.ParseExpression("this.GetValue<" + propertyType + ">(" + name + ")");
        var setter = Syntax.ParseExpression("this.SetValue(" + name + ", value)");

        return node.WithType(propertyType)
                   .WithAccessorList(
            Syntax.AccessorList(Syntax.List(
                Syntax.AccessorDeclaration(
                    SyntaxKind.GetAccessorDeclaration,
                    Syntax.Block(Syntax.ReturnStatement(getter))),
                Syntax.AccessorDeclaration(
                    SyntaxKind.SetAccessorDeclaration,
                    Syntax.Block(Syntax.ExpressionStatement(setter)))))));
    }
}
class PropertyConverter:SyntaxRewriter
{
公共重写SyntaxNode VisitPropertyDeclaration(PropertyDeclarationSyntax节点)
{
如果(!MeetUpdate条件(节点))返回base.VisitPropertyDeclaration(节点);
//这些实现只返回一个字符串
var name=FigureOutName(node.Identifier);
var propertyType=FigureOutType(node.Type);
var getter=Syntax.ParseExpression(“this.GetValue(“+name+”)”);
var setter=Syntax.ParseExpression(“this.SetValue(“+name+”,value)”);
返回节点.WithType(propertyType)
.WithAccessorList(
Syntax.AccessorList(Syntax.List(
Syntax.AccessorDeclaration(
SyntaxKind.GetAccessorDeclaration,
Syntax.Block(Syntax.ReturnStatement(getter)),
Syntax.AccessorDeclaration(
SyntaxKind.SetAccessorDeclaration,
Syntax.Block(Syntax.ExpressionStatement(setter‘‘‘‘‘‘‘‘)’);
}
}
此转换器的结果是一个具有更新属性的类,并在以下代码中输出:

// IDocument csfile <- from a project in a Workspace
var tree = csfile.GetSyntaxTree();
var root = new PropertyConverter().Visit((SyntaxNode)tree.GetRoot())
                                  .NormalizeWhitespace(); // problem!

File.WriteAllText(Path.GetFileName(csfile.FilePath), root.ToFullString());    

//IDocument csfile我想这可能是Roslyn的一个bug。但是,一般来说,我建议在Roslyn.Services.dll中包含的SyntaxNode上使用
格式
扩展方法(使用Roslyn.Services添加


NormalizeWhitespace
是一个非常暴力的系统,其设计主要是为了确保代码能够往返。Roslyn.Services层中存在的格式化代码更灵活一些,并且包含了Visual Studio
格式化文档
命令的许多行为。

输出仍然不太正确,但比
规范化空白
更接近我的期望。您能告诉我有什么不同吗?这可能只是一个bug。如果不存在新行,则不会在成员和XML文档之间添加新行,而且如果两个成员声明在调用之前没有用新行分隔,那么之后也不会添加新行。
/// <summary>
        /// Gets or sets the thickness (TH).
        /// </summary>
public float Thickness
{
    get
    {
        return this.GetValue<float>(TH);
    }

    set
    {
        this.SetValue(TH, value);
    }
}
/// <summary>
        /// Initializes a new instance of the <see cref = "X"/> class.
        /// </summary>
        /// <param name = "innerRadius">Inner radius of the X.</param>
        /// <param name = "thickness">Thickness of the X.</param>