title: How to unbox a C# object to dynamic type
body: <p>I'm trying to do something like this:</p>
  
  <pre><code>void someMethod(TypeA object) { ... }
  
  void someMethod(TypeB object) { ... }
  
  object getObject()
  {
      if (...) return new TypeA();
      else return new TypeB();
  }
  
  object obj = getObject();
  (obj.GetType()) obj;  // won't compile
  someMethod(obj);
  </code></pre>
  
  <p>Obviously I'm confused here.  I know I could make this work by just writing out a conditional statement -- </p>
  
  <pre><code>if (obj.GetType() == typeof(TypeA)) obj = (TypeA)obj;
  else if (obj.GetType() == typeof(TypeB)) obj = (TypeB)obj;
  </code></pre>
  
  <p>-- but isn't there some way to do this at runtime?</p>
  
  <p><strong>EDIT</strong>
  I agree it seems like perhaps not the best design choice, so here's the context.  The point of the above code is Repository base class for Mongo DB.  I want it to be able to handle different kinds of tables.  So, someMethod() is actually <em>remove</em>; and TypeA and TypeB are <em>ObjectID</em> and <em>Guid</em>; the code at the bottom is part of a type-agnostic <em>remove</em> method that accepts the ID as a string; and getObject() is a method to parse the ID parameter.</p>