IndentedTextWriter Class

 

Provides a text writer that can indent new lines by a tab string token.

Namespace:   System.CodeDom.Compiler
Assembly:  System (in System.dll)

System.Object
  System.MarshalByRefObject
    System.IO.TextWriter
      System.CodeDom.Compiler.IndentedTextWriter

public class IndentedTextWriter : TextWriter

NameDescription
System_CAPS_pubmethodIndentedTextWriter(TextWriter)

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

System_CAPS_pubmethodIndentedTextWriter(TextWriter, String)

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

NameDescription
System_CAPS_pubpropertyEncoding

Gets the encoding for the text writer to use.(Overrides TextWriter.Encoding.)

System_CAPS_pubpropertyFormatProvider

Gets an object that controls formatting.(Inherited from TextWriter.)

System_CAPS_pubpropertyIndent

Gets or sets the number of spaces to indent.

System_CAPS_pubpropertyInnerWriter

Gets the TextWriter to use.

System_CAPS_pubpropertyNewLine

Gets or sets the new line character to use.(Overrides TextWriter.NewLine.)

NameDescription
System_CAPS_pubmethodClose()

Closes the document being written to.(Overrides TextWriter.Close().)

System_CAPS_pubmethodCreateObjRef(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.)

System_CAPS_pubmethodDispose()

Releases all resources used by the TextWriter object.(Inherited from TextWriter.)

System_CAPS_protmethodDispose(Boolean)

Releases the unmanaged resources used by the TextWriter and optionally releases the managed resources.(Inherited from TextWriter.)

System_CAPS_pubmethodEquals(Object)

Determines whether the specified object is equal to the current object.(Inherited from Object.)

System_CAPS_protmethodFinalize()

Allows an object to try to free resources and perform other cleanup operations before it is reclaimed by garbage collection.(Inherited from Object.)

System_CAPS_pubmethodFlush()

Flushes the stream.(Overrides TextWriter.Flush().)

System_CAPS_pubmethodFlushAsync()

Asynchronously clears all buffers for the current writer and causes any buffered data to be written to the underlying device. (Inherited from TextWriter.)

System_CAPS_pubmethodGetHashCode()

Serves as the default hash function. (Inherited from Object.)

System_CAPS_pubmethodGetLifetimeService()

Retrieves the current lifetime service object that controls the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_pubmethodGetType()

Gets the Type of the current instance.(Inherited from Object.)

System_CAPS_pubmethodInitializeLifetimeService()

Obtains a lifetime service object to control the lifetime policy for this instance.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodMemberwiseClone()

Creates a shallow copy of the current Object.(Inherited from Object.)

System_CAPS_protmethodMemberwiseClone(Boolean)

Creates a shallow copy of the current MarshalByRefObject object.(Inherited from MarshalByRefObject.)

System_CAPS_protmethodOutputTabs()

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

System_CAPS_pubmethodToString()

Returns a string that represents the current object.(Inherited from Object.)

System_CAPS_pubmethodWrite(Boolean)

Writes the text representation of a Boolean value to the text stream.(Overrides TextWriter.Write(Boolean).)

System_CAPS_pubmethodWrite(Char)

Writes a character to the text stream.(Overrides TextWriter.Write(Char).)

System_CAPS_pubmethodWrite(Char[])

Writes a character array to the text stream.(Overrides TextWriter.Write(Char[]).)

System_CAPS_pubmethodWrite(Char[], Int32, Int32)

Writes a subarray of characters to the text stream.(Overrides TextWriter.Write(Char[], Int32, Int32).)

System_CAPS_pubmethodWrite(Decimal)

Writes the text representation of a decimal value to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWrite(Double)

Writes the text representation of a Double to the text stream.(Overrides TextWriter.Write(Double).)

System_CAPS_pubmethodWrite(Int32)

Writes the text representation of an integer to the text stream.(Overrides TextWriter.Write(Int32).)

System_CAPS_pubmethodWrite(Int64)

Writes the text representation of an 8-byte integer to the text stream.(Overrides TextWriter.Write(Int64).)

System_CAPS_pubmethodWrite(Object)

Writes the text representation of an object to the text stream.(Overrides TextWriter.Write(Object).)

System_CAPS_pubmethodWrite(Single)

Writes the text representation of a Single to the text stream.(Overrides TextWriter.Write(Single).)

System_CAPS_pubmethodWrite(String)

Writes the specified string to the text stream.(Overrides TextWriter.Write(String).)

System_CAPS_pubmethodWrite(String, Object)

Writes out a formatted string, using the same semantics as specified.(Overrides TextWriter.Write(String, Object).)

System_CAPS_pubmethodWrite(String, Object, Object)

Writes out a formatted string, using the same semantics as specified.(Overrides TextWriter.Write(String, Object, Object).)

System_CAPS_pubmethodWrite(String, Object, Object, Object)

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

System_CAPS_pubmethodWrite(String, Object[])

Writes out a formatted string, using the same semantics as specified.(Overrides TextWriter.Write(String, Object[]).)

System_CAPS_pubmethodWrite(UInt32)

Writes the text representation of a 4-byte unsigned integer to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWrite(UInt64)

Writes the text representation of an 8-byte unsigned integer to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteAsync(Char)

Writes a character to the text string or stream asynchronously.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteAsync(Char[])

Writes a character array to the text string or stream asynchronously.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteAsync(Char[], Int32, Int32)

Writes a subarray of characters to the text string or stream asynchronously. (Inherited from TextWriter.)

System_CAPS_pubmethodWriteAsync(String)

Writes a string to the text string or stream asynchronously.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLine()

Writes a line terminator.(Overrides TextWriter.WriteLine().)

System_CAPS_pubmethodWriteLine(Boolean)

Writes the text representation of a Boolean, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Boolean).)

System_CAPS_pubmethodWriteLine(Char)

Writes a character, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Char).)

System_CAPS_pubmethodWriteLine(Char[])

Writes a character array, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Char[]).)

System_CAPS_pubmethodWriteLine(Char[], Int32, Int32)

Writes a subarray of characters, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Char[], Int32, Int32).)

System_CAPS_pubmethodWriteLine(Decimal)

Writes the text representation of a decimal value followed by a line terminator to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLine(Double)

Writes the text representation of a Double, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Double).)

System_CAPS_pubmethodWriteLine(Int32)

Writes the text representation of an integer, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Int32).)

System_CAPS_pubmethodWriteLine(Int64)

Writes the text representation of an 8-byte integer, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Int64).)

System_CAPS_pubmethodWriteLine(Object)

Writes the text representation of an object, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Object).)

System_CAPS_pubmethodWriteLine(Single)

Writes the text representation of a Single, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(Single).)

System_CAPS_pubmethodWriteLine(String)

Writes the specified string, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(String).)

System_CAPS_pubmethodWriteLine(String, Object)

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.(Overrides TextWriter.WriteLine(String, Object).)

System_CAPS_pubmethodWriteLine(String, Object, Object)

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.(Overrides TextWriter.WriteLine(String, Object, Object).)

System_CAPS_pubmethodWriteLine(String, Object, Object, Object)

Writes out a formatted string and a new line, using the same semantics as Format.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLine(String, Object[])

Writes out a formatted string, followed by a line terminator, using the same semantics as specified.(Overrides TextWriter.WriteLine(String, Object[]).)

System_CAPS_pubmethodWriteLine(UInt32)

Writes the text representation of a UInt32, followed by a line terminator, to the text stream.(Overrides TextWriter.WriteLine(UInt32).)

System_CAPS_pubmethodWriteLine(UInt64)

Writes the text representation of an 8-byte unsigned integer followed by a line terminator to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineAsync()

Writes a line terminator asynchronously to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineAsync(Char)

Writes a character followed by a line terminator asynchronously to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineAsync(Char[])

Writes an array of characters followed by a line terminator asynchronously to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineAsync(Char[], Int32, Int32)

Writes a subarray of characters followed by a line terminator asynchronously to the text string or stream.(Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineAsync(String)

Writes a string followed by a line terminator asynchronously to the text string or stream. (Inherited from TextWriter.)

System_CAPS_pubmethodWriteLineNoTabs(String)

Writes the specified string to a line without tabs.

NameDescription
System_CAPS_protfieldCoreNewLine

Stores the newline characters used for this TextWriter.(Inherited from TextWriter.)

System_CAPS_pubfieldSystem_CAPS_staticDefaultTabString

Specifies the default tab string. This field is constant.

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.

System_CAPS_noteNote

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.

The following code example demonstrates using an IndentedTextWriter to write text at different levels of indentation.

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());
        }
    }
}

SecurityAction.LinkDemand

for full trust for the immediate caller. This class cannot be used by partially trusted code.

SecurityAction.InheritanceDemand

for full trust for inheritors. This class cannot be inherited by partially trusted code.

.NET Framework
Available since 1.1

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Return to top
Show: