title: Dynamically get a DbSet<T> by Entity class name
body: <p>I'm trying to use <code>System.Reflections</code> to get a <code>DbSet&lt;T&gt;</code> dynamically from its name.</p>
  
  <p>What I've got right now is:</p>
  
  <ul>
  <li>The <code>DbSet</code> name</li>
  <li>The <code>DbSet</code>'s <code>Type</code> stored on a variable</li>
  </ul>
  
  <p>The issue I'm facing comes out when trying to use the <code>dbcontext.Set&lt;T&gt;()</code> method, since (these are my tries so far):</p>
  
  <ul>
  <li>When I try to assign to <code>&lt;T&gt;</code> my <code>DbSet</code> <code>Type</code>, it throws me the following compilation error:
  
  <blockquote>
    <p>"XXX is a variable but is used like a type"</p>
  </blockquote></li>
  <li>If I try with using both the <code>Extension</code> methods that you will find below in my code (which I made in order to try to get an <code>IQueryable&lt;T&gt;</code>), it returns a <code>IQueryable&lt;object&gt;</code>, which unfortunately is not what I am looking for, since of course when I try to manipulate it with further Reflections, it lacks of all the properties that the original class has…</li>
  </ul>
  
  <p>What am I doing wrong? How can I get a <code>DbSet&lt;T&gt;</code>?</p>
  
  <p>My code is the following, but of course, let me know if you need more infos, clarifications or code snippets.</p>
  
  <p><strong>My Controller's Method</strong>:</p>
  
  <pre><code>public bool MyMethod (string t, int id, string jsonupdate)
  {
      string _tableName = t;
      Type _type = TypeFinder.FindType(_tableName); //returns the correct type
  
      //FIRST TRY
      //throws error: "_type is a variable but is used like a type"
      var tableSet = _context.Set&lt;_type&gt;();  
  
      //SECOND TRY
      //returns me an IQueryable&lt;object&gt;, I need an IQueryable&lt;MyType&gt;
      var tableSet2 = _context.Set(_type);  
  
      //THIRD TRY
      //always returns me am IQueryable&lt;object&gt;, I need an IQueryable&lt;MyType&gt;
      var calcInstance = Activator.CreateInstance(_type);
      var _tableSet3 = _context.Set2(calcInstance);
  
      //...
  }
  </code></pre>
  
  <p>Class <strong>ContextSetExtension</strong></p>
  
  <pre><code>public static class ContextSetExtension
  { 
      public static IQueryable&lt;object&gt; Set(this DbContext _context, Type t)
      {
          var res= _context.GetType().GetMethod("Set").MakeGenericMethod(t).Invoke(_context, null);
          return (IQueryable&lt;object&gt;)res;
      }
  
  
      public static IQueryable&lt;T&gt;Set2&lt;T&gt;(this DbContext _context, T t) 
      {
          var typo = t.GetType();
          return (IQueryable&lt;T&gt;)_context.GetType().GetMethod("Set").MakeGenericMethod(typo).Invoke(_context, null);
      }
  }
  </code></pre>
  
  <p><strong>EDIT</strong> Added <strong>TypeFinder</strong>'s inner code. 
  <br> In brief, this method does the same of <code>Type.GetType</code>, but searches Type on ALL the generated assemblies</p>
  
  <pre><code>public class TypeFinder
  {
  
      public TypeFinder()
      {
      }
      public static Type FindType(string name)
      {
  
  
          Assembly[] assemblies = AppDomain.CurrentDomain.GetAssemblies();
          var result = (from elem in (from app in assemblies
                                          select (from tip in app.GetTypes()
                                                  where tip.Name == name.Trim()
                                                  select tip).FirstOrDefault())
                        where elem != null
                        select elem).FirstOrDefault();
  
          return result;
      }
  }
  </code></pre>
  
  <p><strong>UPDATE</strong> as requested in the comments, here's the specific case:</p>
  
  <p>In my DB i've got some tables which are really similar each other, so the idea was to create a dynamic table-update method which would be good for every table, just passing to this method the table name, the ID of the row to update and the JSON containing data to update. 
  <br> So, in brief, I would perform some updates on the table given in input as DbSet type, updating the row with <code>ID==id</code> in input with the data contained inside the JSON, which will be parsed inside an object of type X(the same of dbset)/into a dictionary.</p>
  
  <p><strong>In pseudo-code:</strong></p>
  
  <pre><code>public bool MyMethod (string t, int id, string jsonupdate)
  {
      string _tableName = t;
      Type _type = TypeFinder.FindType(_tableName); //returns the correct type
  
      //THIS DOESN'T WORKS, of course, since as said above:
      //&lt;&lt;throws error: "_type is a variable but is used like a type"&gt;&gt;
      var tableSet = _context.Set&lt;_type&gt;();  
  
      //parsing the JSON
      var newObj = Newtonsoft.Json.JsonConvert.DeserializeObject(jsonupdate, _type);
  
      //THIS OF COURSE DOESN'T WORKS TOO
      //selecting the row to update:
      var toUpdate = tableSet.Where(x =&gt; x.Id == id).FirstOrDefault();
  
      if(toUpdate!=null)
      {
  
         var newProperties = newObj.GetType().GetProperties();
         var toUpdateProperties = toUpdate.GetType().GetProperties();
  
         foreach(var item in properties)
         {
             var temp = toUpdateProperties.Where(p =&gt; p.Name==item.Name)
             {
                //I write it really in briefand fast, without lots of checks.
                //I think this is enough, I hope
                temp.SetValue(toUpdate, item.GetValue());
             }
         }
  
         _context.SaveChanges();
      }
  
      return false;
  }
  </code></pre>