title: Forcing the use of a factory object to instantiate a class
body: <p>I have the special case where an object needs to be a singleton on a per thread basis. So I'd like to use a static factory method of a <code>Factory</code> class to instantiate those instances. To ensure that the factory class is used (since it caches per thread) the constructor needs to be protected.</p>
  
  <p>So let's say I have a class like this.</p>
  
  <pre><code>public class XXXX : Model {
      protected XXXX() {
      }
  }
  </code></pre>
  
  <p>I would like to use a factory class like this.</p>
  
  <pre><code>public class Factory {
  
       private static Dictionary&lt;int,Model&gt; _singletons;
  
       public static T Instance() where T : Model {
            int thread = Thread.CurrentThread.ManagedThreadId;
            if(!_singletons.ContainsKey(thread))
            {
                _singletons[thread] = new T();
            }
            return (T)_singletons[thread];
       }
  }
  </code></pre>
  
  <p>Then later I can get a reference to each singleton like this, and the reference will be unique for each thread.</p>
  
  <pre><code>XXXX m = Factory.Instance&lt;XXXX&gt;();
  </code></pre>
  
  <p>How can I do this so that the <code>Factory</code> class has access to create instances. One issue is that classes will be defined in other DLLs that will be loaded at run-time. All I can know is that they are derived from <code>Model</code> and have <code>protected/private</code> constructors.</p>