IndentedTextWriter 类

定义

提供可根据 Tab 字符串标记缩进新行的文本编写器。Provides a text writer that can indent new lines by a tab string token.

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如何使用在不同的缩进级别写入文本。The following code example demonstrates using an IndentedTextWriter to write text at different levels of indentation.

#using <System.Drawing.dll>
#using <System.Windows.Forms.dll>
#using <System.dll>

using namespace System;
using namespace System::CodeDom;
using namespace System::CodeDom::Compiler;
using namespace System::ComponentModel;
using namespace System::IO;
using namespace System::Windows::Forms;
public ref class Form1: public System::Windows::Forms::Form
{
private:
   System::Windows::Forms::TextBox^ textBox1;

   String^ CreateMultilevelIndentString()
   {
      
      // Creates a TextWriter to use as the base output writer.
      System::IO::StringWriter^ baseTextWriter = gcnew 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 = gcnew 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();
   }


   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: {0}", level );
      
      // 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: {0}", level );
   }


   void button1_Click( Object^ /*sender*/, System::EventArgs^ /*e*/ )
   {
      textBox1->Text = CreateMultilevelIndentString();
   }


public:
   Form1()
   {
      System::Windows::Forms::Button^ button1 = gcnew System::Windows::Forms::Button;
      this->textBox1 = gcnew 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 = System::Drawing::Point( 8, 40 );
      this->textBox1->Multiline = true;
      this->textBox1->Name = "textBox1";
      this->textBox1->Size = System::Drawing::Size( 391, 242 );
      this->textBox1->TabIndex = 0;
      this->textBox1->Text = "";
      button1->Location = System::Drawing::Point( 11, 8 );
      button1->Name = "button1";
      button1->Size = System::Drawing::Size( 229, 23 );
      button1->TabIndex = 1;
      button1->Text = "Generate string using IndentedTextWriter";
      button1->Click += gcnew System::EventHandler( this, &Form1::button1_Click );
      this->AutoScaleBaseSize = System::Drawing::Size( 5, 13 );
      this->ClientSize = 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]
int main()
{
   Application::Run( gcnew Form1 );
}

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

注解

IndentedTextWriterTextWriter通过提供插入制表符字符串并跟踪当前缩进级别的方法来扩展。IndentedTextWriter extends a TextWriter by providing methods that insert a tab string and track the current indentation level. 用多个缩进级别设置格式的文本对于生成的代码非常有用, 因此此类由 CodeDOM 代码生成器实现使用。Text formatted with multiple indentation levels is useful for generated code, so this class is used by CodeDOM code generator implementations.

选项卡字符串是每个缩进包含的字符串。The tab string is the string that each indentation consists of. 通常, 选项卡字符串包含空白。Typically the tab string contains white space.

备注

此类包含应用于所有成员的类级别的链接要求和继承要求。This class contains a link demand and an inheritance demand at the class level that applies to all members. 当直接调用方或派生类不具有完全信任权限时,将引发。SecurityExceptionA SecurityException is thrown when either the immediate caller or the derived class does not have full-trust permission. 有关安全要求的详细信息, 请参阅链接要求继承要求For details about security demands, see Link Demands and Inheritance Demands.

构造函数

IndentedTextWriter(TextWriter)

使用指定的文本编写器和默认 Tab 字符串,初始化 IndentedTextWriter 类的新实例。Initializes a new instance of the IndentedTextWriter class using the specified text writer and default tab string.

IndentedTextWriter(TextWriter, String)

使用指定的文本编写器和 Tab 字符串初始化 IndentedTextWriter 类的新实例。Initializes a new instance of the IndentedTextWriter class using the specified text writer and tab string.

字段

CoreNewLine

存储用于此 TextWriter 的换行符。Stores the newline characters used for this TextWriter.

(继承自 TextWriter)
DefaultTabString

指定默认 Tab 字符串。Specifies the default tab string. 此字段为常数。This field is constant.

属性

Encoding

获取供文本编写器使用的编码。Gets the encoding for the text writer to use.

FormatProvider

获取控制格式设置的对象。Gets an object that controls formatting.

(继承自 TextWriter)
Indent

获取或设置缩进的空格数。Gets or sets the number of spaces to indent.

InnerWriter

获取要使用的 TextWriterGets the TextWriter to use.

NewLine

获取或设置使用的换行符。Gets or sets the new line character to use.

方法

Close()

关闭正在向其写入的文档。Closes the document being written to.

CreateObjRef(Type)

创建一个对象,该对象包含生成用于与远程对象进行通信的代理所需的全部相关信息。Creates an object that contains all the relevant information required to generate a proxy used to communicate with a remote object.

(继承自 MarshalByRefObject)
Dispose()

释放由 TextWriter 对象使用的所有资源。Releases all resources used by the TextWriter object.

(继承自 TextWriter)
Dispose(Boolean)

释放由 TextWriter 占用的非托管资源,还可以另外再释放托管资源。Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

(继承自 TextWriter)
DisposeAsync()

异步释放由 TextWriter 对象使用的所有资源。Asynchronously releases all resources used by the TextWriter object.

(继承自 TextWriter)
Equals(Object)

确定指定的对象是否等于当前对象。Determines whether the specified object is equal to the current object.

(继承自 Object)
Flush()

刷新流。Flushes the stream.

FlushAsync()

异步清理当前编写器的所有缓冲区,使所有缓冲数据写入基础设备。Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

(继承自 TextWriter)
GetHashCode()

作为默认哈希函数。Serves as the default hash function.

(继承自 Object)
GetLifetimeService()

检索控制此实例的生存期策略的当前生存期服务对象。Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(继承自 MarshalByRefObject)
GetType()

获取当前实例的 TypeGets the Type of the current instance.

(继承自 Object)
InitializeLifetimeService()

获取生存期服务对象来控制此实例的生存期策略。Obtains a lifetime service object to control the lifetime policy for this instance.

(继承自 MarshalByRefObject)
MemberwiseClone()

创建当前 Object 的浅表副本。Creates a shallow copy of the current Object.

(继承自 Object)
MemberwiseClone(Boolean)

创建当前 MarshalByRefObject 对象的浅表副本。Creates a shallow copy of the current MarshalByRefObject object.

(继承自 MarshalByRefObject)
OutputTabs()

根据 Indent 属性为每个缩进级别输出一次 Tab 字符串。Outputs the tab string once for each level of indentation according to the Indent property.

ToString()

返回表示当前对象的字符串。Returns a string that represents the current object.

(继承自 Object)
Write(Boolean)

将 Boolean 值的文本表示形式写入文本流。Writes the text representation of a Boolean value to the text stream.

Write(Char)

将字符写入文本流。Writes a character to the text stream.

Write(Char[])

将字符数组写入文本流。Writes a character array to the text stream.

Write(Char[], Int32, Int32)

将字符的子数组写入文本流。Writes a subarray of characters to the text stream.

Write(Decimal)

将小数值的文本表示形式写入文本流。Writes the text representation of a decimal value to the text stream.

(继承自 TextWriter)
Write(Double)

将 Double 的文本表示形式写入文本流。Writes the text representation of a Double to the text stream.

Write(Int32)

将整数的文本表示形式写入文本流。Writes the text representation of an integer to the text stream.

Write(Int64)

将 8 字节整数的文本表示形式写入文本流。Writes the text representation of an 8-byte integer to the text stream.

Write(Object)

将对象的文本表示形式写入文本流。Writes the text representation of an object to the text stream.

Write(ReadOnlySpan<Char>)

将字符范围写入文本流。Writes a character span to the text stream.

(继承自 TextWriter)
Write(Single)

将 Single 的文本表示形式写入文本流。Writes the text representation of a Single to the text stream.

Write(String)

将指定的字符串写入文本流。Writes the specified string to the text stream.

Write(String, Object)

使用与指定语义相同的语义写出已格式化的字符串。Writes out a formatted string, using the same semantics as specified.

Write(String, Object, Object)

使用与指定语义相同的语义写出已格式化的字符串。Writes out a formatted string, using the same semantics as specified.

Write(String, Object, Object, Object)

使用与 Format(String, Object, Object, Object) 方法相同的语义将格式化字符串写入文本流。Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

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

使用与指定语义相同的语义写出已格式化的字符串。Writes out a formatted string, using the same semantics as specified.

Write(StringBuilder)

将字符串生成器写入文本流。Writes a string builder to the text stream.

(继承自 TextWriter)
Write(UInt32)

将 4 字节无符号整数的文本表示形式写入文本流。Writes the text representation of a 4-byte unsigned integer to the text stream.

(继承自 TextWriter)
Write(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流。Writes the text representation of an 8-byte unsigned integer to the text stream.

(继承自 TextWriter)
WriteAsync(Char)

将字符异步写入文本流。Writes a character to the text stream asynchronously.

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

将字符数组异步写入文本流。Writes a character array to the text stream asynchronously.

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

以异步形式将字符的子数组写入文本流。Writes a subarray of characters to the text stream asynchronously.

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

以异步形式将字符内存区域写入文本流。Asynchronously writes a character memory region to the text stream.

(继承自 TextWriter)
WriteAsync(String)

将字符串异步写入文本流。Writes a string to the text stream asynchronously.

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

以异步形式将字符串生成器写入文本流。Asynchronously writes a string builder to the text stream.

(继承自 TextWriter)
WriteLine()

写入一个行结束符。Writes a line terminator.

WriteLine(Boolean)

将后跟行结束符的 Boolean 的文本表示形式写入文本流。Writes the text representation of a Boolean, followed by a line terminator, to the text stream.

WriteLine(Char)

将后跟行结束符的字符写入文本流。Writes a character, followed by a line terminator, to the text stream.

WriteLine(Char[])

将后跟行结束符的字符数组写入文本流。Writes a character array, followed by a line terminator, to the text stream.

WriteLine(Char[], Int32, Int32)

将后跟行结束符的字符子数组写入文本流。Writes a subarray of characters, followed by a line terminator, to the text stream.

WriteLine(Decimal)

将小数值的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a decimal value to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLine(Double)

将后跟行结束符的 Double 的文本表示形式写入文本流。Writes the text representation of a Double, followed by a line terminator, to the text stream.

WriteLine(Int32)

将后跟行结束符的整数的文本表示形式写入文本流。Writes the text representation of an integer, followed by a line terminator, to the text stream.

WriteLine(Int64)

将后跟行结束符的 8 字节整数的文本表示形式写入文本流。Writes the text representation of an 8-byte integer, followed by a line terminator, to the text stream.

WriteLine(Object)

将后跟行结束符的对象的文本表示形式写入文本流。Writes the text representation of an object, followed by a line terminator, to the text stream.

WriteLine(ReadOnlySpan<Char>)

将字符范围的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a character span to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLine(Single)

将后跟行结束符的 Single 的文本表示形式写入文本流。Writes the text representation of a Single, followed by a line terminator, to the text stream.

WriteLine(String)

将后跟行结束符的指定字符串写入文本流。Writes the specified string, followed by a line terminator, to the text stream.

WriteLine(String, Object)

使用与指定语义相同的语义写出后跟行结束符的已格式化字符串。Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, Object, Object)

使用与指定语义相同的语义写出后跟行结束符的已格式化字符串。Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, Object, Object, Object)

使用与 Format(String, Object) 相同的语义将格式化字符串和新行写入文本流。Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

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

使用与指定语义相同的语义写出后跟行结束符的已格式化字符串。Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(StringBuilder)

将字符串生成器的文本表示形式写入文本流,后跟行终止符。Writes the text representation of a string builder to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLine(UInt32)

将后跟行结束符的 UInt32 的文本表示形式写入文本流。Writes the text representation of a UInt32, followed by a line terminator, to the text stream.

WriteLine(UInt64)

将 8 字节无符号整数的文本表示形式写入文本流,后跟行终止符。Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLineAsync()

以异步形式将行终止符写入文本流。Asynchronously writes a line terminator to the text stream.

(继承自 TextWriter)
WriteLineAsync(Char)

以异步形式将字符写入文本流,后跟行终止符。Asynchronously writes a character to the text stream, followed by a line terminator.

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

以异步形式将字符数组写入文本流,后跟行终止符。Asynchronously writes an array of characters to the text stream, followed by a line terminator.

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

以异步形式将字符子数组写入文本流,后跟行终止符。Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.

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

以异步形式将字符内存区域的文本表示形式写入文本流,后跟行终止符。Asynchronously writes the text representation of a character memory region to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLineAsync(String)

以异步形式将字符串写入文本流,后跟行终止符。Asynchronously writes a string to the text stream, followed by a line terminator.

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

以异步形式将字符串生成器的文本表示形式写入文本流,后跟行终止符。Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.

(继承自 TextWriter)
WriteLineNoTabs(String)

将指定字符串写入无制表符的行。Writes the specified string to a line without tabs.

显式界面实现

IDisposable.Dispose()

有关此成员的说明,请参见 Dispose()For a description of this member, see Dispose().

(继承自 TextWriter)

安全性

LinkDemand
完全信任直接调用方。for full trust for the immediate caller. 此类不能由部分信任的代码使用。This class cannot be used by partially trusted code.

InheritanceDemand
完全信任继承者。for full trust for inheritors. 此类不能由部分信任的代码继承。This class cannot be inherited by partially trusted code.

适用于