title: How to unwrap object handle created with Activator.CreateInstance to the base class?
body: <p>In my previous <a href="https://stackoverflow.com/q/28442519/3481709">question</a> I wanted to force all derived classes to implement their own options class. The given answer provided a solution to my problem but then created another problem as I couldn't unwrap the object handle anymore.</p>
  
  <p>I have created an empty public interface and a class that implements it:</p>
  
  <pre><code>public interface IOptions{};
  
  public class SearchOptions : IOptions
  {
      public Boolean SearchHiddenFiles { get; set; }
      public Boolean CaseSensitive { get; set; }
  }
  </code></pre>
  
  <p>Then I created a base class with an abstract parameter <code>TaskOptions</code> which is type of IOptions:</p>
  
  <pre><code>public abstract class Task&lt;T&gt; where T : IOptions
  {
    public string ID {get; set;}
    public string Name {get; set;}
  
    public abstract T TaskOptions { get; set; }
    public abstract void Execute();
  }
  </code></pre>
  
  <p>And finally, I created bunch of classes which extend Task class, i.e.:</p>
  
  <pre><code>public class TaskSearch : Task &lt;SearchOptions&gt;
  {
      public override SearchOptions TaskOptions { get; set; }
      public override void Execute()
      {
          Console.Write("Search running");
      }
  }
  </code></pre>
  
  <p>The main program initializes a Job object which takes a file that has json notation that defines the id, name, etc.. along with tasks.</p>
  
  <p>Lastly, here is the job class:</p>
  
  <pre><code>public class Job
  {
      public String Name { get; set; }
      public String Description { get; set; }
      public List&lt;Task&lt;IParameters&gt;&gt; Tasks { get; set; }
      public Job(string JsonFile)
      {        
          using (StreamReader file = File.OpenText(JsonFile))
          {
              string json = file.ReadToEnd();
              JObject o = JObject.Parse(json);
  
              this.Name = o.SelectToken("Name").ToString();
              this.Description = o.SelectToken("Description").ToString();
  
              JArray arr = (JArray)o["Tasks"];
  
              foreach (var t in arr)
              {
                  var taskclass = t["Name"].ToString();
  
                  var taskobj = Activator.CreateInstance("RunTasks", "RunTasks." + taskclass);
  
                  var task = (Task&lt;IParameters&gt;)taskobj.Unwrap(); //&lt;-- throws System.InvalidCastException because taskobj is TaskSearch
                  // set task.ID, task.Name, etc..
                  //...
                  //...
                  Tasks.Add(task);
              }
          }
      }
  }
  </code></pre>
  
  <p>Before I changed the Task class to implement IOptions interface, I unwrapped it as:</p>
  
  <pre><code>var task = (Task)taskobj.Unwrap();
  </code></pre>
  
  <p>which worked but after adding <code>IOptions</code> and casting it to <code>Task&lt;IOptions&gt;</code> it throws System.InvalidCastException error because taskobj is TaskSearch (not Task).</p>
  
  <p>If I manually type <code>Task&lt;SearchOptions&gt;</code> then it unwraps but then complains about the <em>Tasks</em> list at the end as it's now not type of <code>Task&lt;IOptions&gt;</code>.
  I need to make the <em>Tasks</em> list generic so it accepts all derived classes and find a way to get type parameters before unwrapping taskobj or get it from <em>taskclass</em> string which holds the current task name.</p>
  
  <p>I'll create a dictionary for the options if I have to but this is how I wanted to implement it and I'd like to know if I can.</p>
  
  <p>Thank you</p>