Επεξεργασία

Κοινή χρήση μέσω


IndentedTextWriter Class

Definition

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
Inheritance
IndentedTextWriter

Examples

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

Remarks

IndentedTextWriter extends a TextWriter by providing methods that insert a tab string and track the current indentation level. 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.

Note

This class contains a link demand and an inheritance demand at the class level that applies to all members. A 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.

Constructors

IndentedTextWriter(TextWriter, String)

Initializes a new instance of the IndentedTextWriter class using the specified text writer and tab string.

IndentedTextWriter(TextWriter)

Initializes a new instance of the IndentedTextWriter class using the specified text writer and default tab string.

Fields

CoreNewLine

Stores the newline characters used for this TextWriter.

(Inherited from TextWriter)
DefaultTabString

Specifies the default tab string. This field is constant.

Properties

Encoding

Gets the encoding for the text writer to use.

FormatProvider

Gets an object that controls formatting.

(Inherited from TextWriter)
Indent

Gets or sets the number of spaces to indent.

InnerWriter

Gets the TextWriter to use.

NewLine

Gets or sets the new line character to use.

Methods

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.

(Inherited from MarshalByRefObject)
Dispose()

Releases all resources used by the TextWriter object.

(Inherited from TextWriter)
Dispose(Boolean)

Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.

(Inherited from TextWriter)
DisposeAsync()

Performs application-defined tasks associated with freeing, releasing, or resetting unmanaged resources asynchronously.

DisposeAsync()

Asynchronously releases all resources used by the TextWriter object.

(Inherited from TextWriter)
Equals(Object)

Determines whether the specified object is equal to the current object.

(Inherited from Object)
Flush()

Flushes the stream.

FlushAsync()

Clears all buffers for this IndentedTextWriter asynchronously and causes any buffered data to be written to the underlying device.

FlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

(Inherited from TextWriter)
FlushAsync(CancellationToken)

Clears all buffers for this IndentedTextWriter asynchronously and causes any buffered data to be written to the underlying device.

FlushAsync(CancellationToken)

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device.

(Inherited from TextWriter)
GetHashCode()

Serves as the default hash function.

(Inherited from Object)
GetLifetimeService()
Obsolete.

Retrieves the current lifetime service object that controls the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
GetType()

Gets the Type of the current instance.

(Inherited from Object)
InitializeLifetimeService()
Obsolete.

Obtains a lifetime service object to control the lifetime policy for this instance.

(Inherited from MarshalByRefObject)
MemberwiseClone()

Creates a shallow copy of the current Object.

(Inherited from Object)
MemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.

(Inherited from MarshalByRefObject)
OutputTabs()

Outputs the tab string once for each level of indentation according to the Indent property.

OutputTabsAsync()

Asynchronously outputs tabs to the underlying TextWriter based on the current Indent.

ToString()

Returns a string that represents the current object.

(Inherited from Object)
Write(Boolean)

Writes the text representation of a Boolean value to the text stream.

Write(Char)

Writes a character to the text stream.

Write(Char[], Int32, Int32)

Writes a subarray of characters to the text stream.

Write(Char[])

Writes a character array to the text stream.

Write(Decimal)

Writes the text representation of a decimal value to the text stream.

(Inherited from TextWriter)
Write(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)

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.

(Inherited from TextWriter)
Write(Single)

Writes the text representation of a Single to the text stream.

Write(String, Object, Object, Object)

Writes a formatted string to the text stream, using the same semantics as the Format(String, Object, Object, Object) method.

(Inherited from TextWriter)
Write(String, Object, Object)

Writes out a formatted string, using the same semantics as specified.

Write(String, Object)

Writes out a formatted string, using the same semantics as specified.

Write(String, Object[])

Writes out a formatted string, using the same semantics as specified.

Write(String, ReadOnlySpan<Object>)

Writes out a formatted string, using the same semantics as specified.

Write(String, ReadOnlySpan<Object>)

Writes a formatted string to the text stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

(Inherited from TextWriter)
Write(String)

Writes the specified string to the text stream.

Write(StringBuilder)

Writes a string builder to the text stream.

(Inherited from TextWriter)
Write(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text stream.

(Inherited from TextWriter)
Write(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text stream.

(Inherited from TextWriter)
WriteAsync(Char)

Asynchronously writes the specified Char to the underlying TextWriter, inserting tabs at the start of every line.

WriteAsync(Char)

Writes a character to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(Char[], Int32, Int32)

Asynchronously writes the specified number of Chars from the specified buffer to the underlying TextWriter, starting at the specified index, and outputting tabs at the start of every new line.

WriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(Char[])

Writes a character array to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the specified characters to the underlying TextWriter, inserting tabs at the start of every line.

WriteAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes a character memory region to the text stream.

(Inherited from TextWriter)
WriteAsync(String)

Asynchronously writes the specified string to the underlying TextWriter, inserting tabs at the start of every line.

WriteAsync(String)

Writes a string to the text stream asynchronously.

(Inherited from TextWriter)
WriteAsync(StringBuilder, CancellationToken)

Asynchronously writes the contents of the specified StringBuilder to the underlying TextWriter, inserting tabs at the start of every line.

WriteAsync(StringBuilder, CancellationToken)

Asynchronously writes a string builder to the text stream.

(Inherited from TextWriter)
WriteLine()

Writes a line terminator.

WriteLine(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[], Int32, Int32)

Writes a subarray of characters, 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(Decimal)

Writes the text representation of a decimal value to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(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)

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.

(Inherited from TextWriter)
WriteLine(Single)

Writes the text representation of a Single, followed by a line terminator, to the text stream.

WriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, Object).

(Inherited from TextWriter)
WriteLine(String, Object, Object)

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, Object)

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, Object[])

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, ReadOnlySpan<Object>)

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.

WriteLine(String, ReadOnlySpan<Object>)

Writes out a formatted string and a new line to the text stream, using the same semantics as Format(String, ReadOnlySpan<Object>).

(Inherited from TextWriter)
WriteLine(String)

Writes the specified string, followed by a line terminator, to the text stream.

WriteLine(StringBuilder)

Writes the text representation of a string builder to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLine(UInt32)

Writes the text representation of a UInt32, followed by a line terminator, to the text stream.

WriteLine(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync()

Asynchronously writes the line terminator to the underlying TextWriter.

WriteLineAsync()

Asynchronously writes a line terminator to the text stream.

(Inherited from TextWriter)
WriteLineAsync(Char)

Asynchronously writes the specified Char to the underlying TextWriter followed by a line terminator, inserting tabs at the start of every line.

WriteLineAsync(Char)

Asynchronously writes a character to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(Char[], Int32, Int32)

Asynchronously writes the specified number of characters from the specified buffer followed by a line terminator, to the underlying TextWriter, starting at the specified index within the buffer, inserting tabs at the start of every line.

WriteLineAsync(Char[], Int32, Int32)

Asynchronously writes a subarray of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(Char[])

Asynchronously writes an array of characters to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the specified characters followed by a line terminator to the underlying TextWriter, inserting tabs at the start of every line.

WriteLineAsync(ReadOnlyMemory<Char>, CancellationToken)

Asynchronously writes the text representation of a character memory region to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(String)

Asynchronously writes the specified string followed by a line terminator to the underlying TextWriter, inserting tabs at the start of every line.

WriteLineAsync(String)

Asynchronously writes a string to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineAsync(StringBuilder, CancellationToken)

Asynchronously writes the contents of the specified StringBuilder followed by a line terminator to the underlying TextWriter, inserting tabs at the start of every line.

WriteLineAsync(StringBuilder, CancellationToken)

Asynchronously writes the text representation of a string builder to the text stream, followed by a line terminator.

(Inherited from TextWriter)
WriteLineNoTabs(String)

Writes the specified string to a line without tabs.

WriteLineNoTabsAsync(String)

Asynchronously writes the specified string to the underlying TextWriter without inserting tabs.

Explicit Interface Implementations

IDisposable.Dispose()

For a description of this member, see Dispose().

(Inherited from TextWriter)

Extension Methods

ConfigureAwait(IAsyncDisposable, Boolean)

Configures how awaits on the tasks returned from an async disposable will be performed.

Applies to