通过


IndentedTextWriter 类

定义

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

public ref class IndentedTextWriter : System::IO::TextWriter
public class IndentedTextWriter : System.IO.TextWriter
type IndentedTextWriter = class
    inherit TextWriter
Public Class IndentedTextWriter
Inherits TextWriter
继承
IndentedTextWriter

示例

下面的代码示例演示如何使用 IndentedTextWriter 不同级别的缩进编写文本。

using System;
using System.CodeDom;
using System.CodeDom.Compiler;
using System.ComponentModel;
using System.IO;
using System.Windows.Forms;

namespace IndentedTextWriterExample
{
    public class Form1 : System.Windows.Forms.Form
    {
        private System.Windows.Forms.TextBox textBox1;

        private string CreateMultilevelIndentString()
        {
            // Creates a TextWriter to use as the base output writer.
            System.IO.StringWriter baseTextWriter = new System.IO.StringWriter();

            // Create an IndentedTextWriter and set the tab string to use
            // as the indentation string for each indentation level.
            System.CodeDom.Compiler.IndentedTextWriter indentWriter = new IndentedTextWriter(baseTextWriter, "    ");

            // Sets the indentation level.
            indentWriter.Indent = 0;

            // Output test strings at stepped indentations through a recursive loop method.
            WriteLevel(indentWriter, 0, 5);

            // Return the resulting string from the base StringWriter.
            return baseTextWriter.ToString();
        }

        private void WriteLevel(IndentedTextWriter indentWriter, int level, int totalLevels)
        {
            // Output a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());

            // If not yet at the highest recursion level, call this output method for the next level of indentation.
            if( level < totalLevels )
            {
                // Increase the indentation count for the next level of indented output.
                indentWriter.Indent++;

                // Call the WriteLevel method to write test output for the next level of indentation.
                WriteLevel(indentWriter, level+1, totalLevels);

                // Restores the indentation count for this level after the recursive branch method has returned.
                indentWriter.Indent--;
            }
            else
            {
                // Outputs a string using the WriteLineNoTabs method.
                indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.");
            }

            // Outputs a test string with a new-line character at the end.
            indentWriter.WriteLine("This is a test phrase. Current indentation level: "+level.ToString());
        }

        private void button1_Click(object sender, System.EventArgs e)
        {
            textBox1.Text = CreateMultilevelIndentString();
        }

        public Form1()
        {
            System.Windows.Forms.Button button1 = new System.Windows.Forms.Button();
            this.textBox1 = new System.Windows.Forms.TextBox();
            this.SuspendLayout();
            this.textBox1.Anchor = ((System.Windows.Forms.AnchorStyles)((((System.Windows.Forms.AnchorStyles.Top | System.Windows.Forms.AnchorStyles.Bottom)
                | System.Windows.Forms.AnchorStyles.Left)
                | System.Windows.Forms.AnchorStyles.Right)));
            this.textBox1.Location = new System.Drawing.Point(8, 40);
            this.textBox1.Multiline = true;
            this.textBox1.Name = "textBox1";
            this.textBox1.Size = new System.Drawing.Size(391, 242);
            this.textBox1.TabIndex = 0;
            this.textBox1.Text = "";
            button1.Location = new System.Drawing.Point(11, 8);
            button1.Name = "button1";
            button1.Size = new System.Drawing.Size(229, 23);
            button1.TabIndex = 1;
            button1.Text = "Generate string using IndentedTextWriter";
            button1.Click += new System.EventHandler(this.button1_Click);
            this.AutoScaleBaseSize = new System.Drawing.Size(5, 13);
            this.ClientSize = new System.Drawing.Size(407, 287);
            this.Controls.Add(button1);
            this.Controls.Add(this.textBox1);
            this.Name = "Form1";
            this.Text = "IndentedTextWriter example";
            this.ResumeLayout(false);
        }

        [STAThread]
        static void Main()
        {
            Application.Run(new Form1());
        }
    }
}
Imports System.CodeDom
Imports System.CodeDom.Compiler
Imports System.ComponentModel
Imports System.IO
Imports System.Windows.Forms

Public Class Form1
   Inherits System.Windows.Forms.Form
   Private textBox1 As System.Windows.Forms.TextBox 
   
   Private Function CreateMultilevelIndentString() As String
        ' Create a TextWriter to use as the base output writer.
        Dim baseTextWriter As New System.IO.StringWriter
      
        ' Create an IndentedTextWriter and set the tab string to use 
        ' as the indentation string for each indentation level.
        Dim indentWriter = New IndentedTextWriter(baseTextWriter, "    ")

        ' Set the indentation level.
        indentWriter.Indent = 0

        ' Output test strings at stepped indentations through a recursive loop method.
        WriteLevel(indentWriter, 0, 5)
      
        ' Return the resulting string from the base StringWriter.
        Return baseTextWriter.ToString()
    End Function

    Private Sub WriteLevel(ByVal indentWriter As IndentedTextWriter, ByVal level As Integer, ByVal totalLevels As Integer)
        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))

        ' If not yet at the highest recursion level, call this output method for the next level of indentation.
        If level < totalLevels Then
            ' Increase the indentation count for the next level of indented output.
            indentWriter.Indent += 1

            ' Call the WriteLevel method to write test output for the next level of indentation.
            WriteLevel(indentWriter, level + 1, totalLevels)

            ' Restores the indentation count for this level after the recursive branch method has returned.
            indentWriter.Indent -= 1

        Else
            ' Output a string using the WriteLineNoTabs method.
            indentWriter.WriteLineNoTabs("This is a test phrase written with the IndentTextWriter.WriteLineNoTabs method.")
        End If

        ' Outputs a test string with a new-line character at the end.
        indentWriter.WriteLine(("This is a test phrase. Current indentation level: " + level.ToString()))
    End Sub

    Private Sub button1_Click(ByVal sender As Object, ByVal e As System.EventArgs)
        textBox1.Text = CreateMultilevelIndentString()
    End Sub

    Public Sub New()
        Dim button1 As New System.Windows.Forms.Button
        Me.textBox1 = New System.Windows.Forms.TextBox
        Me.SuspendLayout()
        Me.textBox1.Anchor = CType(System.Windows.Forms.AnchorStyles.Top Or System.Windows.Forms.AnchorStyles.Bottom Or System.Windows.Forms.AnchorStyles.Left Or System.Windows.Forms.AnchorStyles.Right, System.Windows.Forms.AnchorStyles)
        Me.textBox1.Location = New System.Drawing.Point(8, 40)
        Me.textBox1.Multiline = True
        Me.textBox1.Name = "textBox1"
        Me.textBox1.Size = New System.Drawing.Size(391, 242)
        Me.textBox1.TabIndex = 0
        Me.textBox1.Text = ""
        button1.Location = New System.Drawing.Point(11, 8)
        button1.Name = "button1"
        button1.Size = New System.Drawing.Size(229, 23)
        button1.TabIndex = 1
        button1.Text = "Generate string using IndentedTextWriter"
        AddHandler button1.Click, AddressOf Me.button1_Click
        Me.AutoScaleBaseSize = New System.Drawing.Size(5, 13)
        Me.ClientSize = New System.Drawing.Size(407, 287)
        Me.Controls.Add(button1)
        Me.Controls.Add(Me.textBox1)
        Me.Name = "Form1"
        Me.Text = "IndentedTextWriter example"
        Me.ResumeLayout(False)
    End Sub

    <STAThread()> _
    Shared Sub Main()
        Application.Run(New Form1)
    End Sub
End Class

注解

IndentedTextWriter TextWriter通过提供插入制表符字符串并跟踪当前缩进级别的方法来扩展 a。 使用多个缩进级别格式化的文本对于生成的代码非常有用,因此 CodeDOM 代码生成器实现会使用此类。

Tab 字符串是每个缩进包含的字符串。 通常,选项卡字符串包含空格。

注意

此类包含应用于所有成员的类级别的链接需求和继承需求。 当直接调用者或派生类没有完全信任权限时,将引起 SecurityException。 有关安全需求的详细信息,请参阅 链接需求

构造函数

名称 说明
IndentedTextWriter(TextWriter, String)

使用指定的文本编写器和制表符字符串初始化类的新实例 IndentedTextWriter

IndentedTextWriter(TextWriter)

使用指定的文本编写器和默认制表符字符串初始化类的新实例 IndentedTextWriter

字段

名称 说明
CoreNewLine

存储用于此 TextWriter的换行符。

(继承自 TextWriter)
DefaultTabString

指定默认选项卡字符串。 此字段为常量。

属性

名称 说明
Encoding

获取要使用的文本编写器的编码。

FormatProvider

获取一个对象,该对象控制格式设置。

(继承自 TextWriter)
Indent

获取或设置要缩进的空格数。

InnerWriter

获取 TextWriter 要使用的值。

NewLine

获取或设置要使用的新行字符。

方法

名称 说明
Close()

关闭要写入的文档。

CreateObjRef(Type)

创建一个对象,其中包含生成用于与远程对象通信的代理所需的所有相关信息。

(继承自 MarshalByRefObject)
Dispose()

释放对象使用 TextWriter 的所有资源。

(继承自 TextWriter)
Dispose(Boolean)

释放由托管资源使用 TextWriter 的非托管资源,并选择性地释放托管资源。

(继承自 TextWriter)
DisposeAsync()

以异步方式执行与释放、释放或重置非托管资源关联的应用程序定义任务。

DisposeAsync()

异步释放对象使用 TextWriter 的所有资源。

(继承自 TextWriter)
Equals(Object)

确定指定的对象是否等于当前对象。

(继承自 Object)
Flush()

刷新流。

FlushAsync()

以异步方式清除所有 IndentedTextWriter 缓冲区,并导致任何缓冲数据写入基础设备。

FlushAsync()

异步清除当前编写器的所有缓冲区,并导致任何缓冲数据写入基础设备。

(继承自 TextWriter)
FlushAsync(CancellationToken)

以异步方式清除所有 IndentedTextWriter 缓冲区,并导致任何缓冲数据写入基础设备。

GetHashCode()

用作默认哈希函数。

(继承自 Object)
GetLifetimeService()
已过时.

检索控制此实例的生存期策略的当前生存期服务对象。

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 Type

(继承自 Object)
InitializeLifetimeService()
已过时.

获取生存期服务对象来控制此实例的生存期策略。

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object的浅表副本。

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。

(继承自 MarshalByRefObject)
OutputTabs()

根据 Indent 属性为每个级别的缩进输出一次制表符字符串。

OutputTabsAsync()

根据当前Indent属性异步将选项卡输出到基础TextWriter

ToString()

返回一个表示当前对象的字符串。

(继承自 Object)
Write(Boolean)

将布尔值的文本表示形式写入文本流。

Write(Char)

将字符写入文本流。

Write(Char[], Int32, Int32)

将字符的子数组写入文本流。

Write(Char[])

将字符数组写入文本流。

Write(Decimal)

将小数值的文本表示形式写入文本流。

(继承自 TextWriter)
Write(Double)

将 Double 的文本表示形式写入文本流。

Write(Int32)

将整数的文本表示形式写入文本流。

Write(Int64)

将 8 字节整数的文本表示形式写入文本流。

Write(Object)

将对象的文本表示形式写入文本流。

Write(ReadOnlySpan<Char>)

将字符范围写入文本流。

(继承自 TextWriter)
Write(Rune)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
Write(Single)

将单个的文本表示形式写入文本流。

Write(String, Object, Object, Object)

使用与方法相同的语义 Format(String, Object, Object, Object) 将格式化字符串写入文本流。

(继承自 TextWriter)
Write(String, Object, Object)

使用指定的相同语义写出格式化字符串。

Write(String, Object)

使用指定的相同语义写出格式化字符串。

Write(String, Object[])

使用指定的相同语义写出格式化字符串。

Write(String, ReadOnlySpan<Object>)

使用指定的相同语义写出格式化字符串。

Write(String)

将指定的字符串写入文本流。

Write(StringBuilder)

将字符串生成器写入文本流。

(继承自 TextWriter)
Write(UInt32)

将 4 字节无符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
Write(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流。

(继承自 TextWriter)
WriteAsync(Char)

以异步方式将指定的 Char 值写入基础 TextWriter,在每个行的开头插入制表符。

WriteAsync(Char)

以异步方式将字符写入文本流。

(继承自 TextWriter)
WriteAsync(Char[], Int32, Int32)

从指定的缓冲区异步写入基础TextWriter的指定数目Char,从指定的索引开始,并在每个新行的开头输出选项卡。

WriteAsync(Char[], Int32, Int32)

以异步方式将字符的子数组写入文本流。

(继承自 TextWriter)
WriteAsync(Char[])

以异步方式将字符数组写入文本流。

(继承自 TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

将指定的字符异步写入基础 TextWriter,在每个行的开头插入制表符。

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域异步写入文本流。

(继承自 TextWriter)
WriteAsync(Rune)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteAsync(String, CancellationToken)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteAsync(String)

将指定的字符串异步写入基础 TextWriter,在每个行的开头插入制表符。

WriteAsync(String)

以异步方式将字符串写入文本流。

(继承自 TextWriter)
WriteAsync(StringBuilder, CancellationToken)

将指定 StringBuilder 的内容异步写入基础 TextWriter,在每个行的开头插入制表符。

WriteAsync(StringBuilder, CancellationToken)

将字符串生成器异步写入文本流。

(继承自 TextWriter)
WriteLine()

写入行终止符。

WriteLine(Boolean)

将布尔值(后跟行终止符)的文本表示形式写入文本流。

WriteLine(Char)

将字符(后跟行终止符)写入文本流。

WriteLine(Char[], Int32, Int32)

将字符的子数组(后跟行终止符)写入文本流。

WriteLine(Char[])

将字符数组(后跟行终止符)写入文本流。

WriteLine(Decimal)

将小数值的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Double)

将 Double 的文本表示形式(后跟行终止符)写入文本流。

WriteLine(Int32)

将整数的文本表示形式(后跟行终止符)写入文本流。

WriteLine(Int64)

将 8 字节整数的文本表示形式(后跟行终止符)写入文本流。

WriteLine(Object)

将对象的文本表示形式(后跟行终止符)写入文本流。

WriteLine(ReadOnlySpan<Char>)

将字符范围的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(Rune)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteLine(Single)

将单个的文本表示形式(后跟行终止符)写入文本流。

WriteLine(String, Object, Object, Object)

使用与 相同的语义 Format(String, Object)将格式化字符串和新行写出文本流。

(继承自 TextWriter)
WriteLine(String, Object, Object)

使用指定的相同语义写出格式化字符串,后跟行终止符。

WriteLine(String, Object)

使用指定的相同语义写出格式化字符串,后跟行终止符。

WriteLine(String, Object[])

使用指定的相同语义写出格式化字符串,后跟行终止符。

WriteLine(String, ReadOnlySpan<Object>)

使用指定的相同语义写出格式化字符串,后跟行终止符。

WriteLine(String)

将指定的字符串(后跟行终止符)写入文本流。

WriteLine(StringBuilder)

将字符串生成器的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLine(UInt32)

将 UInt32 的文本表示形式(后跟行终止符)写入文本流。

WriteLine(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync()

将行终止符异步写入基础 TextWriter

WriteLineAsync()

将行终止符异步写入文本流。

(继承自 TextWriter)
WriteLineAsync(CancellationToken)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteLineAsync(Char)

以异步方式将指定的 Char 值写入基础 TextWriter ,后跟行终止符,在每个行的开头插入制表符。

WriteLineAsync(Char)

将字符异步写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(Char[], Int32, Int32)

从指定缓冲区后跟行终止符的指定字符数异步写入基础 TextWriter,从缓冲区内的指定索引处开始,在每个行的开头插入制表符。

WriteLineAsync(Char[], Int32, Int32)

将字符的子数组异步写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(Char[])

将字符数组异步写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

以异步方式将指定字符后跟行终止符写入基础 TextWriter,并在每行开头插入制表符。

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

将字符内存区域的文本表示形式异步写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(Rune)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteLineAsync(String, CancellationToken)

提供文本编写器,该编写器可以通过制表符字符串标记缩进新行。

(继承自 TextWriter)
WriteLineAsync(String)

以异步方式将指定字符串后跟行终止符写入基础 TextWriter,并在每行开头插入制表符。

WriteLineAsync(String)

将字符串异步写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

以异步方式将指定 StringBuilder 内容后跟行终止符写入基础 TextWriter,并在每一行的开头插入制表符。

WriteLineAsync(StringBuilder, CancellationToken)

以异步方式将字符串生成器的文本表示形式写入文本流,后跟行终止符。

(继承自 TextWriter)
WriteLineNoTabs(String)

将指定的字符串写入没有制表符的行。

WriteLineNoTabsAsync(String)

在不插入选项卡的情况下,以异步方式将指定的字符串写入基础 TextWriter

显式接口实现

名称 说明
IDisposable.Dispose()

有关此成员的说明,请参阅 Dispose()

(继承自 TextWriter)

扩展方法

名称 说明
ConfigureAwait(IAsyncDisposable, Boolean)

配置如何执行从异步可释放项返回的任务的 await。

适用于