Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>As others said, extensions methods are compiler magic, you can alway use VS right click, go to definition to find the real type that implements the static method.</p> <p>From there, it gets <strong>fairly hairy</strong>. <code>Where</code> is overloaded, so you need to find the actual definition that matches the signature you want. <code>GetMethod</code> has some limitations with generic types so you have to find the actual one using a search. </p> <p>Once you find the method, you must make the <code>MethodInfo</code> specific using the <code>MakeGenericMethod</code> call.</p> <p>Here is a full working sample: </p> <pre><code>using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Reflection; namespace ConsoleApplication9 { class Program { class MyObject { public string Name { get; set; } } public static void CallWhereMethod() { List&lt;MyObject&gt; myObjects = new List&lt;MyObject&gt;() { new MyObject { Name = "Jon Simpson" }, new MyObject { Name = "Jeff Atwood" } }; Func&lt;MyObject, bool&gt; NameEquals = BuildEqFuncFor&lt;MyObject&gt;("Name", "Jon Simpson"); // The Where method lives on the Enumerable type in System.Linq var whereMethods = typeof(System.Linq.Enumerable) .GetMethods(BindingFlags.Static | BindingFlags.Public) .Where(mi =&gt; mi.Name == "Where"); Console.WriteLine(whereMethods.Count()); // 2 (There are 2 methods that are called Where) MethodInfo whereMethod = null; foreach (var methodInfo in whereMethods) { var paramType = methodInfo.GetParameters()[1].ParameterType; if (paramType.GetGenericArguments().Count() == 2) { // we are looking for Func&lt;TSource, bool&gt;, the other has 3 whereMethod = methodInfo; } } // we need to specialize it whereMethod = whereMethod.MakeGenericMethod(typeof(MyObject)); var ret = whereMethod.Invoke(myObjects, new object[] { myObjects, NameEquals }) as IEnumerable&lt;MyObject&gt;; foreach (var item in ret) { Console.WriteLine(item.Name); } // outputs "Jon Simpson" } public static Func&lt;T, bool&gt; BuildEqFuncFor&lt;T&gt;(string prop, object val) { return t =&gt; t.GetType().InvokeMember(prop, BindingFlags.GetProperty, null, t, null) == val; } static void Main(string[] args) { CallWhereMethod(); Console.ReadKey(); } } } </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload