title: Non-Lazy Static Initialization Block in C#
body: <p>I need to run some code to register a type for a factory pattern. I would do this in Java with a static initialization block or in C++ with a static constructor.</p>
  
  <p>How do you do this in C#? That static constructor gets run lazily and since the type will never be referred to in the code, never gets registered.</p>
  
  <p>EDIT: I tried a test to see the registration code work. This doesn't seem to be working though.</p>
  
  <pre><code>using System;
  using System.Collections.Generic;
  using System.Linq;
  using System.Text;
  
  [assembly: AssemblyTest.RegisterToFactory("hello, world!")]
  
  namespace AssemblyTest
  {
      [AttributeUsage(AttributeTargets.Assembly, Inherited = false, AllowMultiple = true)]
      sealed class RegisterToFactoryAttribute : Attribute
      {
          public RegisterToFactoryAttribute(string name)
          {
              Console.WriteLine("Registered {0}", name);
          }
      }
  
      class Program
      {
          static void Main(string[] args)
          {
          }
      }
  }
  </code></pre>
  
  <p>Nothing gets printed.</p>