Created
July 8, 2025 04:11
-
-
Save f2koi-shiftup/d9d27d27ff8e050d417b835482a1cdb8 to your computer and use it in GitHub Desktop.
Find System.Linq.Queryable.Where<TSource> by reflection
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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