title: convert IEnumerable<T> to IQueryable<T> where T is unknown
body: <p>I have a <code>BindingSource</code> whose <code>DataSource</code> (defined as an object) could be any type of <code>IEnumerable</code> class at runtime (such as <code>IList&lt;Foo&gt;</code>).  I need to convert it to an <code>IQueryable&lt;T&gt;</code> so that I can pass it in to a generic extension:</p>
  
  <pre><code>IOrderedQueryable&lt;TEntity&gt; OrderUsingSortExpression&lt;TEntity&gt;(this IQueryable&lt;TEntity&gt; source, string sortExpression) where TEntity : class
  </code></pre>
  
  <p>So far I have this:</p>
  
  <pre><code>string order = "Message ASC";
  Type thetype = bsTotalBindingSource.DataSource.GetType().GetGenericArguments()[0];
  IEnumerable&lt;object&gt; totalDataSource = ((IEnumerable&lt;object&gt;)(bsTotalBindingSource.DataSource));
  //Blowing up on this next line with 'System.Linq.Queryable is not a GenericTypeDefinition. MakeGenericType may only be called on a type for which Type.IsGenericTypeDefinition is true.'
  MethodInfo asQueryableMethod = typeof(Queryable).MakeGenericType(thetype).GetMethod("AsQueryable", BindingFlags.Static | BindingFlags.Public, null, new[] { typeof(IQueryable&lt;&gt;) }, null); 
  MethodInfo genericAsQueryableMethod = asQueryableMethod.MakeGenericMethod(thetype);
  MethodInfo orderUsingSortExpressionMethod = GetType().GetMethod("OrderUsingSortExpression");
  MethodInfo genericUsingSortExpressionMethod = orderUsingSortExpressionMethod.MakeGenericMethod(thetype);
  bsTotalBindingSource.DataSource = genericUsingSortExpressionMethod.Invoke(this, new object[] { genericAsQueryableMethod.Invoke(totalDataSource, null), order });
  </code></pre>
  
  <p>As you can see, the end goal here is to be able to take something from a DataSource, get its RuntimeType of <code>IEnumerable&lt;T&gt;</code> where <code>T</code> can be whatever, then call <code>AsQueryable&lt;T&gt;</code> so it can be passed into a function which accepts an <code>IQueryable&lt;T&gt;</code>.  </p>
  
  <p>EDIT
  After digging around to find specifically the methods I am looking for I have gotten a bit farther on the problem.  It now looks like this:</p>
  
  <pre><code>string order = "Message ASC";
  Type thetype = bsTotalBindingSource.DataSource.GetType().GetGenericArguments()[0];
  //Returns the AsQueryable&lt;&gt; method I am looking for
  MethodInfo asQueryableMethod = typeof(Queryable).MakeGenericType(thetype).GetMethods()[1]; 
  MethodInfo genericAsQueryableMethod = asQueryableMethod.MakeGenericMethod(thetype);
  MethodInfo orderUsingSortExpressionMethod = typeof(SortExtension)GetType().GetMethods()[0];
  MethodInfo genericUsingSortExpressionMethod = orderUsingSortExpressionMethod.MakeGenericMethod(thetype);
  bsTotalBindingSource.DataSource = genericUsingSortExpressionMethod.Invoke(this, new object[] { genericAsQueryableMethod
  //blows up here with 'Object of type 'System.RuntimeType' cannot be converted to type 'System.Collections.Generic.IEnumerable`1[LogRecordDTO]'.'
  .Invoke(bsTotalBindingSource.DataSource, new object[] {thetype}), order });
  </code></pre>