title: C# How to instantiate inherited objects via a static method?
body: <p>I'm currently working on an isometric tile-based game. Currently, I have a grid with tiles and those objects contain data about their position, type and various objects(like trees) that can be placed on top of them. </p>
  
  <p>I created an abstract TileObject class and there is another class called Tree which inherites from TileObject. My goal is to have a static function which is responsible for creating these TileObjects without using constructor. </p>
  
  <p>I've tried this with Generics too (even using the Activator class) but didn't seem to work.
  My thoughts would be generics, system.type, typeof but I'm unable to put these all together.</p>
  
  <pre><code>public abstract class TileObject
  {
      protected TileObject() { }
  
      public Tile Parent { get; set; }
      public string ObjectType { get; set; }
  
      public static TileObject CreateObject(string objName, Tile parent)
      {
  
      }
  }
  
  public class Tree : TileObject
  {
  
  }
  </code></pre>
  
  <p>This solution could work in my opinion but it seems so hardcoded</p>
  
  <pre><code>public static TileObject CreateObject(string objName, Tile parent)
  {
      TileObject x;
      if(objName == "Tree")
          x = new Tree();
      else if(objName == "Chair")
          ...
  }
  </code></pre>