Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 1042317
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T15:29:21+00:00 2026-05-16T15:29:21+00:00

I want to select the right generic method via reflection and then call it.

  • 0

I want to select the right generic method via reflection and then call it.

Usually this is quite easy. For example

var method = typeof(MyType).GetMethod("TheMethod");
var typedMethod = method.MakeGenericMethod(theTypeToInstantiate);

However the issue start when there are different generic overloads of the method. For example the static-methods in the System.Linq.Queryable-class. There are two definitions of the ‘Where’-method

static IQueryable<T> Where(this IQueryable<T> source, Expression<Func<T,bool>> predicate)
static IQueryable<T> Where(this IQueryable<T> source, Expression<Func<T,int,bool>> predicate)

This meand that GetMethod doesn’t work, because it cannot destiguish the two. Therefore I want to select the right one.

So far I often just took the first or second method, depending on my need. Like this:

var method = typeof (Queryable).GetMethods().First(m => m.Name == "Where");
var typedMethod = method.MakeGenericMethod(theTypeToInstantiate);

However I’m not happy with this, because I make a huge assumption that the first method is the right one. I rather want to find the right method by the argument type. But I couldn’t figure out how.

I tried it with passing the ‘types’, but it didn’t work.

        var method = typeof (Queryable).GetMethod(
            "Where", BindingFlags.Static,
            null,
            new Type[] {typeof (IQueryable<T>), typeof (Expression<Func<T, bool>>)},
            null);

So has anyone an idea how I can find the ‘right’ generic method via reflection. For example the right version of the ‘Where’-method on the Queryable-class?

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T15:29:21+00:00Added an answer on May 16, 2026 at 3:29 pm

    It can be done, but it’s not pretty!

    For example, to get the first overload of Where mentioned in your question you could do this:

    var where1 = typeof(Queryable).GetMethods()
                     .Where(x => x.Name == "Where")
                     .Select(x => new { M = x, P = x.GetParameters() })
                     .Where(x => x.P.Length == 2
                                 && x.P[0].ParameterType.IsGenericType
                                 && x.P[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
                                 && x.P[1].ParameterType.IsGenericType
                                 && x.P[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>))
                     .Select(x => new { x.M, A = x.P[1].ParameterType.GetGenericArguments() })
                     .Where(x => x.A[0].IsGenericType
                                 && x.A[0].GetGenericTypeDefinition() == typeof(Func<,>))
                     .Select(x => new { x.M, A = x.A[0].GetGenericArguments() })
                     .Where(x => x.A[0].IsGenericParameter
                                 && x.A[1] == typeof(bool))
                     .Select(x => x.M)
                     .SingleOrDefault();
    

    Or if you wanted the second overload:

    var where2 = typeof(Queryable).GetMethods()
                     .Where(x => x.Name == "Where")
                     .Select(x => new { M = x, P = x.GetParameters() })
                     .Where(x => x.P.Length == 2
                                 && x.P[0].ParameterType.IsGenericType
                                 && x.P[0].ParameterType.GetGenericTypeDefinition() == typeof(IQueryable<>)
                                 && x.P[1].ParameterType.IsGenericType
                                 && x.P[1].ParameterType.GetGenericTypeDefinition() == typeof(Expression<>))
                     .Select(x => new { x.M, A = x.P[1].ParameterType.GetGenericArguments() })
                     .Where(x => x.A[0].IsGenericType
                                 && x.A[0].GetGenericTypeDefinition() == typeof(Func<,,>))
                     .Select(x => new { x.M, A = x.A[0].GetGenericArguments() })
                     .Where(x => x.A[0].IsGenericParameter
                                 && x.A[1] == typeof(int)
                                 && x.A[2] == typeof(bool))
                     .Select(x => x.M)
                     .SingleOrDefault();
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

i have this lists, and i want select li element from jquery when the
this is the output i want SELECT * FROM wateat_tbl where name like '%love%'
I want to know the proper way to write a query like this: var
I have two DataGirdViews. I want to select rows from right DataGridView and pass
i have users table and i have posts table i want select from users
I want to select an element which was created by the CSS selector :before
I want to select the second <p> tag and style it within a itemize
I want to select the Resources that are used by the most users. The
I want to select all elements that have an accesskey defined on them. I
I want to select every 6th row in the D column. I thought I

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.