title: Explain to me the following VS 2010 Extension Sample code
body: <p>Coders, I am building a VS 2010 extension and I am experimenting around some of the samples that came with the VS 2010 SDK.</p>
  
  <p>One of the sample projects is called TextAdornment. In that project there is a weirdo class that looks like the following:</p>
  
  <pre><code>[Export(typeof(IWpfTextViewCreationListener))]
  [ContentType("text")]
  [TextViewRole(PredefinedTextViewRoles.Document)]
  internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener
  </code></pre>
  
  <p>While I was experimenting with this project, I tried to debug the project to see the flow of the program and I noticed that this class gets hit when I first start the debugging.</p>
  
  <p>Now my question is the following: what makes this class being the first class to get called when VS starts? In other words, why this class gets active and it runs as of some code instantiate an object of this class type?</p>
  
  <p>Here is the only two files in the sample project:</p>
  
  <p><strong>TextAdornment1Factory.cs</strong></p>
  
  <pre><code>using System.ComponentModel.Composition;
  using Microsoft.VisualStudio.Text.Editor;
  using Microsoft.VisualStudio.Utilities;
  
  namespace TextAdornment1
  {
      #region Adornment Factory
      /// &lt;summary&gt;
      /// Establishes an &lt;see cref="IAdornmentLayer"/&gt; to place the adornment on and exports the &lt;see cref="IWpfTextViewCreationListener"/&gt;
      /// that instantiates the adornment on the event of a &lt;see cref="IWpfTextView"/&gt;'s creation
      /// &lt;/summary&gt;
      [Export(typeof(IWpfTextViewCreationListener))]
      [ContentType("text")]
      [TextViewRole(PredefinedTextViewRoles.Document)]
      internal sealed class TextAdornment1Factory : IWpfTextViewCreationListener
      {
          /// &lt;summary&gt;
          /// Defines the adornment layer for the adornment. This layer is ordered 
          /// after the selection layer in the Z-order
          /// &lt;/summary&gt;
          [Export(typeof(AdornmentLayerDefinition))]
          [Name("TextAdornment1")]
          [Order(After = PredefinedAdornmentLayers.Selection, Before = PredefinedAdornmentLayers.Text)]
          [TextViewRole(PredefinedTextViewRoles.Document)]
          public AdornmentLayerDefinition editorAdornmentLayer = null;
  
          /// &lt;summary&gt;
          /// Instantiates a TextAdornment1 manager when a textView is created.
          /// &lt;/summary&gt;
          /// &lt;param name="textView"&gt;The &lt;see cref="IWpfTextView"/&gt; upon which the adornment should be placed&lt;/param&gt;
          public void TextViewCreated(IWpfTextView textView)
          {
              new TextAdornment1(textView);
          }
      }
      #endregion //Adornment Factory
  }
  </code></pre>
  
  <p><strong>TextAdornment1.cs</strong></p>
  
  <pre><code>using System.Windows;
  using System.Windows.Controls;
  using System.Windows.Media;
  using Microsoft.VisualStudio.Text;
  using Microsoft.VisualStudio.Text.Editor;
  using Microsoft.VisualStudio.Text.Formatting;
  
  namespace TextAdornment1
  {
      ///&lt;summary&gt;
      ///TextAdornment1 places red boxes behind all the "A"s in the editor window
      ///&lt;/summary&gt;
      public class TextAdornment1
      {
          IAdornmentLayer _layer;
          IWpfTextView _view;
          Brush _brush;
          Pen _pen;
  
          ITextView textView;
  
          public TextAdornment1(IWpfTextView view)
          {
              _view = view;
              _layer = view.GetAdornmentLayer("TextAdornment1");
              textView = view;
  
              //Listen to any event that changes the layout (text changes, scrolling, etc)
              _view.LayoutChanged += OnLayoutChanged;
              _view.Closed += new System.EventHandler(_view_Closed);
              //selectedText();
  
              //Create the pen and brush to color the box behind the a's
              Brush brush = new SolidColorBrush(Color.FromArgb(0x20, 0x00, 0x00, 0xff));
              brush.Freeze();
              Brush penBrush = new SolidColorBrush(Colors.Red);
              penBrush.Freeze();
              Pen pen = new Pen(penBrush, 0.5);
              pen.Freeze();
  
              _brush = brush;
              _pen = pen;
          }
  
          void _view_Closed(object sender, System.EventArgs e)
          {
              MessageBox.Show(textView.Selection.IsEmpty.ToString());
          }
  
          /// &lt;summary&gt;
          /// On layout change add the adornment to any reformatted lines
          /// &lt;/summary&gt;
          private void OnLayoutChanged(object sender, TextViewLayoutChangedEventArgs e)
          {
              foreach (ITextViewLine line in e.NewOrReformattedLines)
              {
                  this.CreateVisuals(line);
              }
          }
  
          private void selectedText()
          {
  
          }
          /// &lt;summary&gt;
          /// Within the given line add the scarlet box behind the a
          /// &lt;/summary&gt;
          private void CreateVisuals(ITextViewLine line)
          {
              //grab a reference to the lines in the current TextView 
              IWpfTextViewLineCollection textViewLines = _view.TextViewLines;
              int start = line.Start;
              int end = line.End;
  
              //Loop through each character, and place a box around any a 
              for (int i = start; (i &lt; end); ++i)
              {
                  if (_view.TextSnapshot[i] == 'a')
                  {
                      SnapshotSpan span = new SnapshotSpan(_view.TextSnapshot, Span.FromBounds(i, i + 1));
                      Geometry g = textViewLines.GetMarkerGeometry(span);
                      if (g != null)
                      {
                          GeometryDrawing drawing = new GeometryDrawing(_brush, _pen, g);
                          drawing.Freeze();
  
                          DrawingImage drawingImage = new DrawingImage(drawing);
                          drawingImage.Freeze();
  
                          Image image = new Image();
                          image.Source = drawingImage;
  
                          //Align the image with the top of the bounds of the text geometry
                          Canvas.SetLeft(image, g.Bounds.Left);
                          Canvas.SetTop(image, g.Bounds.Top);
  
                          _layer.AddAdornment(AdornmentPositioningBehavior.TextRelative, span, null, image, null);
                      }
                  }
              }
          }
      }
  }
  </code></pre>