title: Reflection + Linq + DbSet
body: <p>I use EF code-first 4.1. in my application. Now I want to get entities through WCF services using generic types. 
  I'm trying to reflect generic type and invoke the method <strong>ToList</strong> of <strong>DbSet</strong> Object.
  Here is my code:</p>
  
  <pre><code> public string GetAllEntries(string objectType)
          {
              try
              {
               var   mdc =
                     Globals.DbConnection.Create(@"some_db_connection", true);
  
                 // Getting assembly for types
                  var asob = Assembly.GetAssembly(typeof(CrmObject));
                 // getting requested object type from assembly
                  var genericType = asob.GetType(objectType, true, true);
  
                  if (genericType.BaseType == typeof(CrmObject))
                  {
                      // Getting Set&lt;T&gt; method
                      var method = mdc.GetType().GetMember("Set").Cast&lt;MethodInfo&gt;().Where(x =&gt; x.IsGenericMethodDefinition).FirstOrDefault();
  
                     // Making Set&lt;SomeRealCrmObject&gt;() method
                      var genericMethod = method.MakeGenericMethod(genericType);
                     // invoking Setmethod into invokeSet 
                      var invokeSet = genericMethod.Invoke(mdc, null);
                     // invoking ToList method from Set&lt;&gt; invokeSet 
                      var invokeToList = invokeSet.GetType().GetMember("ToList").Cast&lt;MethodInfo&gt;().FirstOrDefault();
  
                      //this return not referenced object as result
                      return invokeToList.ToString();
                  }
  
                  return null;
              }
              catch (Exception ex)
              {
                  return ex.Message + Environment.NewLine + ex.StackTrace;
              }
          }
  </code></pre>
  
  <p>In fact then I write the code like <code>return mdc.Set&lt;SomeRealCrmObject&gt;().ToList()</code> - works fine for me! But then I use the generic types I cannot find the method ToList in object <code>DbSet&lt;SomeRealCrmObject&gt;()</code>. </p>