title: Generic static class - retrieving object type in run-time
body: <p>I have an object of type <strong>X</strong>, which I can (obviously) retrieve in runtime.</p>
  
  <pre><code>var type = myObject.GetType();
  </code></pre>
  
  <p>And I have a generic static class.</p>
  
  <pre><code>public static class MyStaticClass&lt;T&gt;
  {
      public static void DoStuff(T something)
      {
          // bla bla
      }
  }
  </code></pre>
  
  <p>What I'd like to do is:</p>
  
  <pre><code>MyStaticClass&lt;myObject.GetType()&gt;.DoStuff(myObject);
  </code></pre>
  
  <p>But I can't.</p>
  
  <p>In fact, there is just a few types on which MyStaticClass would operate, and they share several interfaces. One workaround is to write:</p>
  
  <pre><code>if (myObject.GetType() == typeof(X))
  {
      MyStaticClass&lt;X&gt;.DoStuff(myObject as X);
  }
  if (myObject.GetType() == typeof(Y))
  {
      MyStaticClass&lt;Y&gt;.DoStuff(myObject as Y);
  }
  </code></pre>
  
  <p>but it's verbose, and writing that everywhere is really ugly - I feel that I shouldn't do that but also that I shouldn't <em>have</em> to do that.</p>
  
  <p>I can't believe there is no solution. Or any neater workaround at least? Or is my approach wrong to start with (what's the alternative if so)? Should I create some (abstract?) base class for X, Y, Z?</p>