Skip to content

Instantly share code, notes, and snippets.

@f2koi-shiftup
Created July 8, 2025 04:11
Show Gist options
  • Save f2koi-shiftup/d9d27d27ff8e050d417b835482a1cdb8 to your computer and use it in GitHub Desktop.
Save f2koi-shiftup/d9d27d27ff8e050d417b835482a1cdb8 to your computer and use it in GitHub Desktop.
Find System.Linq.Queryable.Where<TSource> by reflection
public static class A
{
private static MethodInfo WhereMethodInfo()
{
var tSource = typeof(Queryable)
.GetMethods()
.Where(m => m.Name == nameof(Queryable.Where))
.First()
.GetGenericArguments()[0];
var methodInfo = typeof(Queryable)
.GetMethod(
name: "Where",
genericParameterCount: 1,
bindingAttr: BindingFlags.Static | BindingFlags.Public,
types: [typeof(IQueryable<>).MakeGenericType(tSource), typeof(Expression<>).MakeGenericType(typeof(Func<,>).MakeGenericType(tSource, typeof(bool)))]);
return methodInfo ?? throw new UnreachableException("Cannot find System.Linq.Where<TSource>");
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment