title: How to create a base class with static factory method in C#?
body: <p>I have need to use one of two custom file readers classes; one to read a fixed width file and one for a CSV file.  Each of these readers will have certain properties, etc.  I want to use factory methods and have private constructors so I can run some business logic before creating the objects.</p>
  
  <p>EDIT: better examples</p>
  
  <pre><code>//simple class with it's own factory method
  class Class1
  {
      private Class1()
      {
          //constructor code
      }
      public static Class1 CreateClass()
      {
          //do some business logic here
          return new Class1();
      }
  }
  </code></pre>
  
  <p>What I want to be able to do is define a base class, then override the factory.  I guess the problem is that a static class belongs to the base CLASS, so can never be overriden, even though they ARE inherited.  This code works</p>
  
  <pre><code>public class BaseClass
  {
      //some common properties / fields here
      public string SomeField;
  
      //some common methods here
  
      //empty constructor
      protected BaseClass() { }
  
      //cannot have a virtual static class!  
      //Would really like to make this a virtual method
      public static BaseClass CreateClass() 
      {
          throw new NotImplementedException("BaseClass is meant to be derived");
      }
  
      public static string DoCommonStaticThing(){
          return "I don't know why you'd ever do this"; 
      }
  }
  
  public class DerivedClass1 : BaseClass
  {
      //private constructor
      private DerivedClass1() {}
  
      //concrete factory method
      //would really like to say "override" here
      public static BaseClass CreateClass()
      {
          DerivedClass1 d1 = new DerivedClass1();
          d1.SomeField = "I'm a derived class\r\n" + DoCommonStaticThing();
          return d1;
      }
  }
  </code></pre>
  
  <p>EDIT: To clarify further, what I'm trying to do is put some common functionality in my base class, but define an interface for my file-format-specific methods.  Some of the methods are common, but the business logic for the constructor(s) is file format specific.  My code above works, but it seems to me it would be better to mark the base class factory method as virtual, and the derived class factory method as "override".</p>
  
  <p>I tried to do this, but got "A static member cannot be marked as override, virtual, or abstract".</p>
  
  <p>What's the right way to achieve my goals?</p>